]> gitweb.factorcode.org Git - factor.git/blob - basis/gobject-introspection/ffi/ffi.factor
badd24e19fd000f98e151b1326893ba5a5ad9575
[factor.git] / basis / gobject-introspection / ffi / ffi.factor
1 ! Copyright (C) 2010 Anton Gorenko.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors alien.c-types alien.parser arrays ascii
4 classes.parser classes.struct combinators combinators.short-circuit
5 gobject-introspection.repository gobject-introspection.types kernel
6 locals make math.parser namespaces parser sequences
7 splitting.monotonic vocabs.parser words words.constant ;
8 IN: gobject-introspection.ffi
9
10 : def-c-type ( c-type-name base-c-type -- )
11     swap (CREATE-C-TYPE) typedef ;
12
13 : defer-c-type ( c-type-name -- c-type )
14     deferred-type swap (CREATE-C-TYPE) [ typedef ] keep ;
15 !     create-in dup
16 !     [ fake-definition ] [ undefined-def define ] bi ;
17
18 :: defer-types ( types type-info-class -- )
19     types [
20         [ c-type>> defer-c-type ]
21         [ name>> qualified-name ] bi
22         type-info-class new swap register-type
23     ] each ;
24
25 : def-alias-c-type ( base-c-type c-type-name -- c-type )
26     (CREATE-C-TYPE) [ typedef ] keep ;
27
28 : alias-c-type-name ( alias -- c-type-name )
29     ! <workaround for alises w/o c:type (Atk)
30     [ c-type>> ] [ name>> ] bi or ;
31     ! workaround>
32     ! c-type>> ;
33
34 :: def-alias ( alias -- )
35     alias type>> get-type-info
36     [ c-type>> alias alias-c-type-name def-alias-c-type ]
37     [ clone ] bi alias name>> qualified-name register-type ;
38
39 : def-aliases ( aliases -- )
40     [ def-alias ] each ;
41
42 GENERIC: type>c-type ( type -- c-type )
43
44 M: atomic-type type>c-type get-type-info c-type>> ;
45 M: enum-type type>c-type get-type-info c-type>> ;
46 M: bitfield-type type>c-type get-type-info c-type>> ;
47 M: record-type type>c-type get-type-info c-type>> <pointer> ;
48 M: union-type type>c-type get-type-info c-type>> <pointer> ;
49 M: boxed-type type>c-type get-type-info c-type>> <pointer> ;
50 M: callback-type type>c-type get-type-info c-type>> ;
51 M: class-type type>c-type get-type-info c-type>> <pointer> ;
52 M: interface-type type>c-type get-type-info c-type>> <pointer> ;
53
54 M: boxed-array-type type>c-type
55     name>> simple-type new swap >>name type>c-type ;
56
57 M: c-array-type type>c-type
58     element-type>> type>c-type <pointer> ;
59
60 M: fixed-size-array-type type>c-type
61     [ element-type>> type>c-type ] [ fixed-size>> ] bi 2array ;
62
63 ! <workaround for <type/> (in some signals and properties)
64 PREDICATE: incorrect-type < simple-type name>> not ;
65 M: incorrect-type type>c-type drop void* ;
66 ! workaround>
67
68 GENERIC: parse-const-value ( str data-type -- value )
69
70 M: atomic-type parse-const-value
71     name>> {
72         { "gint" [ string>number ] }
73         { "gdouble" [ string>number ] }
74     } case ;
75
76 M: utf8-type parse-const-value drop ;
77
78 : const-value ( const -- value )
79     [ value>> ] [ type>> ] bi parse-const-value ;
80
81 : def-const ( const -- )
82     [ c-identifier>> create-function ] [ const-value ] bi
83     define-constant ;
84
85 : def-consts ( consts -- )
86     [ def-const ] each ;
87
88 : define-enum-member ( member -- )
89     [ c-identifier>> create-function ] [ value>> ] bi
90     define-constant ;
91
92 : def-enum-type ( enum -- )
93     [ members>> [ define-enum-member ] each ]
94     [ c-type>> int def-c-type ] bi ;
95
96 : def-bitfield-type ( bitfield -- )
97     def-enum-type ;
98
99 GENERIC: parameter-type>c-type ( data-type -- c-type )
100
101 M: data-type parameter-type>c-type type>c-type ;
102 M: varargs-type parameter-type>c-type drop void* ;
103
104 : parameter-c-type ( parameter -- c-type )
105     [ type>> parameter-type>c-type ] keep
106     direction>> "in" = [ <pointer> ] unless ;
107
108 GENERIC: return-type>c-type ( data-type -- c-type )
109
110 M: data-type return-type>c-type type>c-type ;
111 M: none-type return-type>c-type drop void ;
112
113 : return-c-type ( return -- c-type )
114     type>> return-type>c-type ;
115
116 : parameter-name ( parameter -- name )
117     dup type>> varargs-type?
118     [ drop "varargs" ] [ name>> "!incorrect-name!" or ] if ;
119
120 : error-parameter ( -- parameter )
121     parameter new
122         "error" >>name
123         "in" >>direction
124         "none" >>transfer-ownership
125         simple-type new "GLib.Error" >>name >>type ;
126
127 : ?suffix-parameters-with-error ( callable -- parameters )
128     [ parameters>> ] [ throws?>> ] bi
129     [ error-parameter suffix ] when ;
130
131 : parameter-names&types ( callable -- names types )
132     [ [ parameter-c-type ] map ] [ [ parameter-name ] map ] bi ;
133
134 : def-function ( function --  )
135     {
136         [ return>> return-c-type ]
137         [ identifier>> ]
138         [ drop current-library get ]
139         [ ?suffix-parameters-with-error parameter-names&types ]
140     } cleave make-function define-inline ;
141
142 : def-functions ( functions -- )
143     [ def-function ] each ;
144
145 GENERIC: type>data-type ( type -- data-type )
146
147 M: type type>data-type
148     [ simple-type new ] dip name>> >>name ;
149
150 : word-started? ( word letter -- ? )
151     [ letter? ] [ LETTER? ] bi* and ; inline
152
153 : camel-case>underscore-separated ( str -- str' )
154     [ word-started? not ] monotonic-split "_" join >lower ;
155
156 : type>parameter-name ( type -- name )
157     name>> camel-case>underscore-separated ;
158
159 : type>parameter ( type -- parameter )
160     [ parameter new ] dip {
161         [ type>parameter-name >>name ]
162         [ type>data-type >>type ]
163         [ drop "in" >>direction "none" >>transfer-ownership ]
164     } cleave ;
165
166 :: def-method ( method type --  )
167     method {
168         [ return>> return-c-type ]
169         [ identifier>> ]
170         [ drop current-library get ]
171         [
172             ?suffix-parameters-with-error
173             type type>parameter prefix
174             parameter-names&types
175         ]
176     } cleave make-function define-inline ;
177
178 : def-methods ( methods type -- )
179     [ def-method ] curry each ;
180
181 : def-callback-type ( callback -- )
182     {
183         [ drop current-library get ]
184         [ return>> return-c-type ]
185         [ c-type>> ]
186         [ ?suffix-parameters-with-error parameter-names&types ]
187     } cleave make-callback-type define-inline ;
188
189 GENERIC: field-type>c-type ( data-type -- c-type )
190
191 M: simple-type field-type>c-type type>c-type ;
192 M: inner-callback-type field-type>c-type drop void* ;
193 M: array-type field-type>c-type type>c-type ;
194
195 : field>struct-slot ( field -- slot )
196     [ name>> ]
197     [ dup bits>> [ drop uint ] [ type>> field-type>c-type ] if ]
198     [
199         [
200             [ drop ] ! [ writable?>> [ read-only , ] unless ]
201             [ bits>> [ bits: , , ] when* ] bi
202         ] V{ } make
203     ] tri <struct-slot-spec> ;
204
205 : def-record-type ( record -- )
206     dup fields>>
207     [
208         [ c-type>> create-class-in ]
209         [ fields>> [ field>struct-slot ] map ] bi
210         define-struct-class
211     ] [ c-type>> void def-c-type ] if ;
212
213 : def-record ( record -- )
214     {
215         [ def-record-type ]
216         [ constructors>> def-functions ]
217         [ functions>> def-functions ]
218         [ [ methods>> ] keep def-methods ]
219     } cleave ;
220
221 : def-union-type ( union -- )
222     c-type>> void def-c-type ;
223
224 : private-record? ( record -- ? )
225     {
226         [ struct-for>> ]
227         [ name>> "Class" tail? ]
228         [ name>> "Private" tail? ]
229         [ name>> "Iface" tail? ]
230     } 1|| ;
231
232 : def-union ( union -- )
233     {
234         [ def-union-type ]
235         [ constructors>> def-functions ]
236         [ functions>> def-functions ]
237         [ [ methods>> ] keep def-methods ]
238     } cleave ;
239
240 : find-existing-boxed-type ( boxed -- type/f )
241     c-type>> search [
242         dup [ c-type? ] [ "c-type" word-prop ] bi or
243         [ drop f ] unless
244     ] [ f ] if* ;
245
246 : def-boxed-type ( boxed -- )
247     c-type>> void def-c-type ;
248
249 : signal-name ( signal type -- name )
250     swap [ c-type>> ] [ name>> ] bi* ":" glue ;
251
252 : user-data-parameter ( -- parameter )
253     parameter new
254         "user_data" >>name
255         "in" >>direction
256         "none" >>transfer-ownership
257         simple-type new "gpointer" >>name >>type ;
258
259 :: def-signal ( signal type -- )
260     signal {
261         [ drop current-library get ]
262         [ return>> return-c-type ]
263         [ type signal-name ]
264         [
265             parameters>> type type>parameter prefix
266             user-data-parameter suffix parameter-names&types
267         ]
268     } cleave make-callback-type define-inline ;
269     
270 : def-signals ( signals type -- )
271     [ def-signal ] curry each ;
272
273 : def-class-type ( class -- )
274     c-type>> void def-c-type ;
275
276 : def-class ( class -- )
277     {
278         [ def-class-type ]
279         [ constructors>> def-functions ]
280         [ functions>> def-functions ]
281         [ [ methods>> ] keep def-methods ]
282         [ [ signals>> ] keep def-signals ]
283     } cleave ;
284
285 : def-interface-type ( interface -- )
286     c-type>> void def-c-type ;
287
288 : def-interface ( class -- )
289     {
290         [ def-interface-type ]
291         [ functions>> def-functions ]
292         [ [ methods>> ] keep def-methods ]
293         [ [ signals>> ] keep def-signals ]
294     } cleave ;
295
296 : defer-enums ( enums -- ) enum-info defer-types ;
297 : defer-bitfields ( bitfields -- ) bitfield-info defer-types ;
298 : defer-unions ( unions -- ) union-info defer-types ;
299 : defer-callbacks ( callbacks -- ) callback-info defer-types ;
300 : defer-interfaces ( interfaces -- ) interface-info defer-types ;
301 : defer-classes ( class -- ) class-info defer-types ;
302
303 : defer-boxeds ( boxeds -- )
304     [
305         [
306             dup find-existing-boxed-type
307             [ nip ] [ c-type>> defer-c-type ] if*
308         ]
309         [ name>> qualified-name ] bi
310         boxed-info new swap register-type
311     ] each ;
312
313 : defer-records ( records -- )
314     [ private-record? ] partition
315     [ begin-private record-info defer-types end-private ]
316     [ record-info defer-types ] bi* ;
317
318 : def-enums ( enums -- ) [ def-enum-type ] each ;
319 : def-bitfields ( bitfields -- ) [ def-bitfield-type ] each ;
320 : def-unions ( unions -- ) [ def-union ] each ;
321 : def-callbacks ( callbacks -- ) [ def-callback-type ] each ;
322 : def-interfaces ( interfaces -- ) [ def-interface ] each ;
323 : def-classes ( classes -- ) [ def-class ] each ;
324
325 : def-boxeds ( boxeds -- )
326     [ find-existing-boxed-type not ] filter
327     [ def-boxed-type ] each ;
328
329 : def-records ( records -- )
330     [ private-record? ] partition
331     [ begin-private [ def-record ] each end-private ]
332     [ [ def-record ] each ] bi* ;
333
334 : def-namespace ( namespace -- )
335     {
336         [ consts>> def-consts ]
337
338         [ enums>> defer-enums ]
339         [ bitfields>> defer-bitfields ]
340         [ records>> defer-records ]
341         [ unions>> defer-unions ]
342         [ boxeds>> defer-boxeds ]
343         [ callbacks>> defer-callbacks ]
344         [ interfaces>> defer-interfaces ]
345         [ classes>> defer-classes ]
346
347         [ aliases>> def-aliases ]
348
349         [ enums>> def-enums ]
350         [ bitfields>> def-bitfields ]
351         [ records>> def-records ]
352         [ unions>> def-unions ]
353         [ boxeds>> def-boxeds ]
354         [ callbacks>> def-callbacks ]
355         [ interfaces>> def-interfaces ]
356         [ classes>> def-classes ]
357
358         [ functions>> def-functions ]
359     } cleave ;
360
361 : def-ffi-repository ( repository -- )
362     namespace>> def-namespace ;
363