Changes required by SBCL 1.0.22
[clg] / glib / gtype.lisp
CommitLineData
55212af1 1;; Common Lisp bindings for GTK+ v2.x
08cb5756 2;; Copyright 2000-2006 Espen S. Johnsen <espen@users.sf.net>
0d07716f 3;;
55212af1 4;; Permission is hereby granted, free of charge, to any person obtaining
5;; a copy of this software and associated documentation files (the
6;; "Software"), to deal in the Software without restriction, including
7;; without limitation the rights to use, copy, modify, merge, publish,
8;; distribute, sublicense, and/or sell copies of the Software, and to
9;; permit persons to whom the Software is furnished to do so, subject to
10;; the following conditions:
0d07716f 11;;
55212af1 12;; The above copyright notice and this permission notice shall be
13;; included in all copies or substantial portions of the Software.
0d07716f 14;;
55212af1 15;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16;; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17;; MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18;; IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19;; CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20;; TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21;; SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
43c60305 23;; $Id: gtype.lisp,v 1.68 2009/02/09 12:22:53 espen Exp $
0d07716f 24
25(in-package "GLIB")
26
27(use-prefix "g")
28
d1266407 29;; Initialize the glib type system
30(defbinding type-init () nil)
31(type-init)
0d07716f 32
08eab1a3 33(eval-when (:compile-toplevel :load-toplevel :execute)
34 (defbinding (bitsize-of-gtype "bitsize_of_gtype") () unsigned-int))
35
36(deftype type-number () `(unsigned-byte ,(bitsize-of-gtype)))
0d07716f 37
b4a2c852 38(deftype gtype () 'symbol)
39
4d1fea77 40(define-type-method alien-type ((type gtype))
41 (declare (ignore type))
b4a2c852 42 (alien-type 'type-number))
43
08cb5756 44(define-type-method size-of ((type gtype) &key (inlined t))
45 (assert-inlined type inlined)
b4a2c852 46 (size-of 'type-number))
47
08cb5756 48(define-type-method to-alien-form ((type gtype) gtype &optional copy-p)
49 (declare (ignore type copy-p))
b4a2c852 50 `(find-type-number ,gtype t))
51
08cb5756 52(define-type-method to-alien-function ((type gtype) &optional copy-p)
53 (declare (ignore type copy-p))
b4a2c852 54 #'(lambda (gtype)
55 (find-type-number gtype t)))
56
08cb5756 57(define-type-method from-alien-form ((type gtype) form &key ref)
58 (declare (ignore type ref))
59 `(type-from-number ,form))
b4a2c852 60
08cb5756 61(define-type-method from-alien-function ((type gtype) &key ref)
62 (declare (ignore type ref))
b4a2c852 63 #'(lambda (type-number)
40c346ec 64 (type-from-number type-number)))
b4a2c852 65
08cb5756 66(define-type-method writer-function ((type gtype) &key temp (inlined t))
67 (declare (ignore temp))
68 (assert-inlined type inlined)
b4a2c852 69 (let ((writer (writer-function 'type-number)))
70 #'(lambda (gtype location &optional (offset 0))
71 (funcall writer (find-type-number gtype t) location offset))))
72
08cb5756 73(define-type-method reader-function ((type gtype) &key ref (inlined t))
74 (declare (ignore ref))
75 (assert-inlined type inlined)
b4a2c852 76 (let ((reader (reader-function 'type-number)))
08cb5756 77 #'(lambda (location &optional (offset 0))
40c346ec 78 (type-from-number (funcall reader location offset)))))
b4a2c852 79
80
fc47a022 81(eval-when (:compile-toplevel :load-toplevel :execute)
3a935dfa 82 (defclass type-query (struct)
fc47a022 83 ((type-number :allocation :alien :type type-number)
08cb5756 84 (name :allocation :alien :type (copy-of string))
fc47a022 85 (class-size :allocation :alien :type unsigned-int)
86 (instance-size :allocation :alien :type unsigned-int))
4d1d3921 87 (:metaclass struct-class)))
fc47a022 88
89
b4a2c852 90(defbinding type-query (type) nil
91 ((find-type-number type t) type-number)
08cb5756 92 ((make-instance 'type-query) type-query :in/return))
fc47a022 93
94(defun type-instance-size (type)
95 (slot-value (type-query type) 'instance-size))
96
97(defun type-class-size (type)
98 (slot-value (type-query type) 'class-size))
0d07716f 99
3a935dfa 100(defbinding type-class-ref (type) pointer
101 ((find-type-number type t) type-number))
0d07716f 102
08cb5756 103(defbinding type-class-unref () nil
104 (class pointer))
fc47a022 105
3a935dfa 106(defbinding type-class-peek (type) pointer
107 ((find-type-number type t) type-number))
fc47a022 108
0d07716f 109
08cb5756 110
3a935dfa 111;;;; Mapping between lisp types and glib types
0d07716f 112
dcb31db6 113(defvar *registered-types* ())
114(defvar *registered-type-aliases* ())
0f68f696 115(defvar *registered-static-types* ())
dcb31db6 116(defvar *lisp-type-to-type-number* (make-hash-table))
117(defvar *type-number-to-lisp-type* (make-hash-table))
3a935dfa 118
119(defbinding %type-from-name () type-number
120 (name string))
121
dcb31db6 122(defun type-number-from-glib-name (name &optional (error-p t))
123 (let ((type-number (%type-from-name name)))
124 (cond
125 ((not (zerop type-number)) type-number)
126 (error-p (error "Invalid gtype name: ~A" name)))))
127
b29e33dd 128(defun type-from-glib-name (name)
129 (type-from-number (type-number-from-glib-name name) t))
130
7797f30a 131(defun type-registered-p (type)
132 (nth-value 1 (gethash type *lisp-type-to-type-number*)))
133
43c60305 134(defun register-type (type id &optional (error-p t))
08cb5756 135 (cond
7797f30a 136 ((type-registered-p type) (find-type-number type))
08cb5756 137 ((not id) (warn "Can't register type with no foreign id: ~A" type))
138 (t
139 (pushnew (cons type id) *registered-types* :key #'car)
140 (let ((type-number
141 (typecase id
43c60305 142 (string (type-number-from-glib-name id error-p))
08cb5756 143 (symbol (funcall id)))))
144 (setf (gethash type *lisp-type-to-type-number*) type-number)
145 (setf (gethash type-number *type-number-to-lisp-type*) type)
146 type-number))))
dcb31db6 147
148(defun register-type-alias (type alias)
149 (pushnew (cons type alias) *registered-type-aliases* :key #'car)
150 (setf
151 (gethash type *lisp-type-to-type-number*)
152 (find-type-number alias t)))
153
154(defun reinitialize-all-types ()
155 (clrhash *lisp-type-to-type-number*)
156 (clrhash *type-number-to-lisp-type*)
157 (type-init) ; initialize the glib type system
158 (mapc #'(lambda (type)
43c60305 159 (register-type (car type) (cdr type) nil))
dcb31db6 160 *registered-types*)
161 (mapc #'(lambda (type)
08cb5756 162 (apply #'register-new-type type))
b29e33dd 163 (reverse *registered-static-types*))
e9177b70 164 (mapc #'(lambda (type)
dcb31db6 165 (register-type-alias (car type) (cdr type)))
166 *registered-type-aliases*))
167
dcb31db6 168#+cmu
43c60305 169(asdf:install-init-hook 'system::reinitialize-global-table
170 *after-save-initializations*) ; we shouldn't need to do this?
171(asdf:install-init-hook 'reinitialize-all-types)
172
dcb31db6 173
174
175(defun find-type-number (type &optional error-p)
0d07716f 176 (etypecase type
177 (integer type)
dcb31db6 178 (string (type-number-from-glib-name type error-p))
3a935dfa 179 (symbol
dcb31db6 180 (or
181 (gethash type *lisp-type-to-type-number*)
7797f30a 182 (let ((class (find-class type nil)))
183 (when (and class (not (class-finalized-p class)))
184 (finalize-inheritance class)
185 (gethash type *lisp-type-to-type-number*)))
dcb31db6 186 (and error-p (error "Type not registered: ~A" type))))
7797f30a 187 (class
188 (find-type-number (class-name type) error-p))))
0d07716f 189
b011356b 190(defun type-from-number (type-number &optional error)
191 (multiple-value-bind (type found)
dcb31db6 192 (gethash type-number *type-number-to-lisp-type*)
e40a19fb 193 (if found
194 type
dcb31db6 195 (let ((name (find-foreign-type-name type-number)))
e40a19fb 196 (cond
f53fad52 197 ((and name (not (= (type-number-from-glib-name name nil) type-number)))
e40a19fb 198 ;; This is a hack because GdkEvent seems to be registered
199 ;; multiple times
200 (type-from-number (type-number-from-glib-name name)))
201 ((and error name)
202 (error "Type number not registered: ~A (~A)" type-number name))
203 ((and error)
204 (error "Invalid type number: ~A" type-number)))))))
0d07716f 205
dcb31db6 206(defbinding (find-foreign-type-name "g_type_name") (type) (copy-of string)
3a935dfa 207 ((find-type-number type t) type-number))
208
209(defun type-number-of (object)
210 (find-type-number (type-of object) t))
211
cf5bbb0e 212;; For #+(SBCL WIN32):
213;; The first 2 lines of the output from "pexports" are:
214;; LIBRARY XXX.dll
215;; EXPORTS
216;; We don't do anything to skip these 2 lines because they won't pass the
217;; WHEN (AND ...) in the LOOP
218;; - cph 19-May-2007
219
6556dccd 220(eval-when (:compile-toplevel :load-toplevel :execute)
dcb31db6 221 (defvar *type-initializers* ())
7797f30a 222
223 (defun library-filename (system library)
224 (let ((component (asdf:find-component (asdf:find-system system) library)))
225 (etypecase component
226 (asdf:shared-object
227 (first (asdf:output-files (make-instance 'asdf:compile-op) component)))
228 (asdf:library (asdf:component-pathname component)))))
229
dcb31db6 230 (defun %find-types-in-library (pathname prefixes ignore)
cf5bbb0e 231 (let ((outname (tmpname "types")))
6556dccd 232 (unwind-protect
cf5bbb0e 233 (let ((asdf::*verbose-out* nil))
8e6906b3 234 #-win32
cf5bbb0e 235 (asdf:run-shell-command "nm ~A ~A > ~A"
236 #-darwin "--defined-only --dynamic --extern-only"
237 #+darwin "-f -s __TEXT __text"
238 (namestring (truename pathname)) outname)
239 ;; Note about win32 port:
240 ;; 1. (TRUENAME PATHNAME) will bomb.
241 ;; 2. either
242 ;; pexports "d:\\whatever\\bin\\zlib1.dll"
243 ;; or
244 ;; pexports d:/whatever/bin/zlib1.dll
245 ;; anything else will bomb. this is why ~S is used below.
246 #+win32
247 (asdf:run-shell-command "pexports ~S > ~A"
248 (namestring pathname) outname)
249
250 (with-open-file (output outname)
251 (loop
252 as line = (read-line output nil)
253 as symbol = (when line
254 #-win32
255 (let ((pos (position #\space line :from-end t)))
256 #-darwin(subseq line (1+ pos))
257 #+darwin
258 (when (char= (char line (1- pos)) #\T)
259 (subseq line (+ pos 2))))
260 #+win32
261 (subseq line 0 (1- (length line))))
262 while line
263 when (and
264 symbol (> (length symbol) 9)
265 (not (char= (char symbol 0) #\_))
266 (or
267 (not prefixes)
268 (some #'(lambda (prefix)
269 (and
270 (> (length symbol) (length prefix))
271 (string= prefix symbol :end2 (length prefix))))
272 (mklist prefixes)))
273 (string= "_get_type" symbol :start2 (- (length symbol) 9))
274 (not (member symbol ignore :test #'string=)))
275 collect symbol)))
276 (delete-file outname)))))
6556dccd 277
0d07716f 278
7797f30a 279(defun car-eq-p (ob1 ob2)
280 (eq (car ob1) (car ob2)))
281
07dafdb0 282(defmacro init-types-in-library (system library &key prefix ignore)
7797f30a 283 (let* ((filename (library-filename system library))
07dafdb0 284 (names (%find-types-in-library filename prefix ignore)))
6556dccd 285 `(progn
7797f30a 286 ,@(mapcar
287 #'(lambda (name)
288 `(progn
289 (defbinding (,(intern name) ,name) () type-number)
290 (,(intern name))
291 (pushnew (cons ',(intern name) ,filename) *type-initializers*
292 :test #'car-eq-p)))
293 names))))
6556dccd 294
dcb31db6 295(defun find-type-init-function (type-number)
80031aba 296 (loop
7797f30a 297 for (type-init) in *type-initializers*
80031aba 298 when (= type-number (funcall type-init))
299 do (return type-init)))
300
301(defun register-type-as (type-number)
302 (or
303 (find-type-init-function type-number)
304 (find-foreign-type-name type-number)
305 (error "Unknown type-number: ~A" type-number)))
dcb31db6 306
307(defun default-type-init-name (type)
308 (find-symbol (format nil "~A_~A_get_type"
309 (package-prefix *package*)
310 (substitute #\_ #\- (string-downcase type)))))
311
6556dccd 312
0a77b51f 313(eval-when (:compile-toplevel :load-toplevel :execute)
314 (defclass type-info (struct)
315 ((class-size :allocation :alien :type (unsigned 16) :initarg :class-size)
316 (base-init :allocation :alien :type pointer)
317 (base-finalize :allocation :alien :type pointer)
318 (class-init :allocation :alien :type pointer)
319 (class-finalize :allocation :alien :type pointer)
320 (class-data :allocation :alien :type pointer)
321 (instance-size :allocation :alien :type (unsigned 16)
322 :initarg :instance-size)
323 (n-preallocs :allocation :alien :type (unsigned 16))
324 (instance-init :allocation :alien :type pointer)
325 (value-table :allocation :alien :type pointer))
326 (:metaclass struct-class)))
327
328(defbinding %type-register-static () type-number
e40a19fb 329 (parent-type type-number)
0a77b51f 330 (name string)
331 (info type-info)
332 (0 unsigned-int))
333
8fbfa684 334(defun register-new-type (type parent &optional foreign-name)
0a77b51f 335 (let ((parent-info (type-query parent)))
336 (with-slots ((parent-number type-number) class-size instance-size) parent-info
337 (let ((type-number
338 (%type-register-static
339 parent-number
8fbfa684 340 (or foreign-name (default-alien-type-name type))
0a77b51f 341 (make-instance 'type-info :class-size class-size :instance-size instance-size))))
0f68f696 342 (pushnew (list type parent foreign-name) *registered-static-types* :key #'car)
343 (setf (gethash type *lisp-type-to-type-number*) type-number)
344 (setf (gethash type-number *type-number-to-lisp-type*) type)
345 type-number))))
0a77b51f 346
347
6556dccd 348
349;;;; Metaclass for subclasses of ginstance
350
351(eval-when (:compile-toplevel :load-toplevel :execute)
352 (defclass ginstance-class (proxy-class)
7bab08b9 353 ((gtype :initarg :gtype :initform nil :reader ginstance-class-gtype))))
f53fad52 354
355
d905d6ef 356(defun update-size (class)
357 (let ((type-number (find-type-number class)))
358 (cond
08cb5756 359 ((not (foreign-size-p class))
360 (setf (foreign-size class) (type-instance-size type-number)))
d905d6ef 361 ((and
08cb5756 362 (foreign-size-p class)
363 (not (= (type-instance-size type-number) (foreign-size class))))
d905d6ef 364 (warn "Size mismatch for class ~A" class)))))
365
7ce0497d 366
f53fad52 367(defmethod finalize-inheritance ((class ginstance-class))
08cb5756 368 (prog1
369 #+clisp(call-next-method)
370 (let* ((class-name (class-name class))
371 (super (most-specific-proxy-superclass class))
372 (gtype (or
373 (first (ginstance-class-gtype class))
7797f30a 374 (default-alien-type-name class-name))))
375 (unless (type-registered-p class-name)
376 (type-class-ref
377 (if (or (symbolp gtype) (type-number-from-glib-name gtype nil))
378 (register-type class-name gtype)
379 (register-new-type class-name (class-name super) gtype))))
6b716036 380 #+nil
08cb5756 381 (when (and
7797f30a 382 (supertype (find-type-number class))
383 (not (eq (class-name super) (supertype (find-type-number class)))))
08cb5756 384 (warn "Super class mismatch between CLOS and GObject for ~A"
385 class-name)))
386 (update-size class))
387 #-clisp(call-next-method))
d905d6ef 388
389
390(defmethod shared-initialize ((class ginstance-class) names &rest initargs)
08cb5756 391 (declare (ignore names initargs))
d905d6ef 392 (call-next-method)
393 (when (class-finalized-p class)
394 (update-size class)))
395
b011356b 396
6556dccd 397(defmethod validate-superclass ((class ginstance-class) (super standard-class))
398 (subtypep (class-name super) 'ginstance))
399
0d07716f 400
fc47a022 401;;;; Superclass for wrapping types in the glib type system
0d07716f 402
403(eval-when (:compile-toplevel :load-toplevel :execute)
8ac82923 404 (defclass ginstance (ref-counted-object)
7ce0497d 405 (;(class :allocation :alien :type pointer :offset 0)
406 )
407 (:metaclass proxy-class)
408 (:size #.(size-of 'pointer))))
0d07716f 409
08cb5756 410(defun ref-type-number (location &optional offset)
411 (declare (ignore location offset)))
412
413(setf (symbol-function 'ref-type-number) (reader-function 'type-number))
414
609ba905 415(defun %type-number-of-ginstance (location)
08cb5756 416 (let ((class (ref-pointer location)))
417 (ref-type-number class)))
0d07716f 418
08cb5756 419(defmethod make-proxy-instance :around ((class ginstance-class) location
420 &rest initargs)
4d1d3921 421 (declare (ignore class))
609ba905 422 (let ((class (labels ((find-known-class (type-number)
423 (or
424 (find-class (type-from-number type-number) nil)
425 (unless (zerop type-number)
426 (find-known-class (type-parent type-number))))))
427 (find-known-class (%type-number-of-ginstance location)))))
22f85ce9 428 ;; Note that changing the class argument must not alter "the
1d06a422 429 ;; ordered set of applicable methods" as specified in the
430 ;; Hyperspec
4d1d3921 431 (if class
1d06a422 432 (apply #'call-next-method class location initargs)
433 (error "Object at ~A has an unkown type number: ~A"
434 location (%type-number-of-ginstance location)))))
435
0d07716f 436
3a935dfa 437;;;; Registering fundamental types
438
40c346ec 439(register-type 'nil "void")
3a935dfa 440(register-type 'pointer "gpointer")
441(register-type 'char "gchar")
442(register-type 'unsigned-char "guchar")
443(register-type 'boolean "gboolean")
3a935dfa 444(register-type 'int "gint")
0b392a0d 445(register-type-alias 'integer 'int)
dcb31db6 446(register-type-alias 'fixnum 'int)
3a935dfa 447(register-type 'unsigned-int "guint")
448(register-type 'long "glong")
449(register-type 'unsigned-long "gulong")
450(register-type 'single-float "gfloat")
451(register-type 'double-float "gdouble")
b011356b 452(register-type 'string "gchararray")
7c9561c0 453(register-type-alias 'pathname 'string)
3a935dfa 454
455
e9934f39 456;;;; Introspection of type information
3a935dfa 457
4812615b 458(defvar *derivable-type-info* (make-hash-table))
3a935dfa 459
e9934f39 460(defun register-derivable-type (type id expander &optional dependencies)
3a935dfa 461 (register-type type id)
4812615b 462 (let ((type-number (register-type type id)))
e9934f39 463 (setf
464 (gethash type-number *derivable-type-info*)
465 (list expander dependencies))))
3a935dfa 466
b011356b 467(defun find-type-info (type)
468 (dolist (super (cdr (type-hierarchy type)))
4812615b 469 (let ((info (gethash super *derivable-type-info*)))
b011356b 470 (return-if info))))
471
e9934f39 472(defun expand-type-definition (type forward-p options)
473 (let ((expander (first (find-type-info type))))
474 (funcall expander (find-type-number type t) forward-p options)))
3a935dfa 475
08cb5756 476
3a935dfa 477(defbinding type-parent (type) type-number
478 ((find-type-number type t) type-number))
479
480(defun supertype (type)
481 (type-from-number (type-parent type)))
482
7858d45e 483(defbinding %type-interfaces (type) pointer
484 ((find-type-number type t) type-number)
485 (n-interfaces unsigned-int :out))
486
487(defun type-interfaces (type)
488 (multiple-value-bind (array length) (%type-interfaces type)
489 (unwind-protect
4d1d3921 490 (map-c-vector 'list #'identity array 'type-number length)
7858d45e 491 (deallocate-memory array))))
492
493(defun implements (type)
494 (mapcar #'type-from-number (type-interfaces type)))
495
3a935dfa 496(defun type-hierarchy (type)
497 (let ((type-number (find-type-number type t)))
498 (unless (= type-number 0)
499 (cons type-number (type-hierarchy (type-parent type-number))))))
500
501(defbinding (type-is-p "g_type_is_a") (type super) boolean
502 ((find-type-number type) type-number)
503 ((find-type-number super) type-number))
504
505(defbinding %type-children () pointer
506 (type-number type-number)
507 (num-children unsigned-int :out))
508
509(defun map-subtypes (function type &optional prefix)
510 (let ((type-number (find-type-number type t)))
511 (multiple-value-bind (array length) (%type-children type-number)
512 (unwind-protect
4d1d3921 513 (map-c-vector
3a935dfa 514 'nil
515 #'(lambda (type-number)
516 (when (or
517 (not prefix)
dcb31db6 518 (string-prefix-p prefix (find-foreign-type-name type-number)))
3a935dfa 519 (funcall function type-number))
520 (map-subtypes function type-number prefix))
521 array 'type-number length)
522 (deallocate-memory array)))))
523
524(defun find-types (prefix)
525 (let ((type-list nil))
4812615b 526 (maphash
527 #'(lambda (type-number expander)
528 (declare (ignore expander))
529 (map-subtypes
530 #'(lambda (type-number)
531 (pushnew type-number type-list))
532 type-number prefix))
533 *derivable-type-info*)
3a935dfa 534 type-list))
535
08cb5756 536(defun find-type-dependencies (type &optional options)
537 (let ((find-dependencies (second (find-type-info type))))
538 (when find-dependencies
539 (remove-duplicates
540 (mapcar #'find-type-number
541 (funcall find-dependencies (find-type-number type t) options))))))
542
543
544;; The argument is a list where each elements is on the form
6b716036 545;; (type . dependencies). This function will not handle indirect
07dafdb0 546;; dependencies and types depending on them selves.
08cb5756 547(defun sort-types-topologicaly (unsorted)
548 (flet ((depend-p (type1)
549 (find-if #'(lambda (type2)
550 (and
551 ;; If a type depends a subtype it has to be
552 ;; forward defined
553 (not (type-is-p (car type2) (car type1)))
554 (find (car type2) (cdr type1))))
555 unsorted)))
556 (let ((sorted
557 (loop
558 while unsorted
559 nconc (multiple-value-bind (sorted remaining)
560 (delete-collect-if
561 #'(lambda (type)
562 (or (not (cdr type)) (not (depend-p type))))
563 unsorted)
564 (cond
565 ((not sorted)
566 ;; We have a circular dependency which have to
567 ;; be resolved
568 (let ((selected
569 (find-if
570 #'(lambda (type)
571 (every
572 #'(lambda (dep)
573 (or
574 (not (type-is-p (car type) dep))
575 (not (find dep unsorted :key #'car))))
576 (cdr type)))
577 unsorted)))
578 (unless selected
579 (error "Couldn't resolve circular dependency"))
580 (setq unsorted (delete selected unsorted))
581 (list selected)))
582 (t
583 (setq unsorted remaining)
584 sorted))))))
585
586 ;; Mark types which have to be forward defined
587 (loop
588 for tmp on sorted
589 as (type . dependencies) = (first tmp)
590 collect (cons type (and
591 dependencies
592 (find-if #'(lambda (type)
593 (find (car type) dependencies))
594 (rest tmp))
595 t))))))
3a935dfa 596
597
7797f30a 598(defun expand-type-definitions (type-list &optional args)
dcb31db6 599 (flet ((type-options (type-number)
600 (let ((name (find-foreign-type-name type-number)))
b011356b 601 (cdr (assoc name args :test #'string=)))))
3a935dfa 602
7797f30a 603 (setq type-list
604 (delete-if
605 #'(lambda (type-number)
606 (let ((name (find-foreign-type-name type-number)))
607 (or
608 (getf (type-options type-number) :ignore)
609 (find-if
610 #'(lambda (options)
611 (and
612 (string-prefix-p (first options) name)
613 (getf (cdr options) :ignore-prefix)
614 (not (some
615 #'(lambda (exception)
616 (string= name exception))
617 (getf (cdr options) :except)))))
618 args))))
619 type-list))
620
621 (dolist (type-number type-list)
622 (let ((name (find-foreign-type-name type-number)))
623 (register-type
624 (getf (type-options type-number) :type (default-type-name name))
625 (register-type-as type-number))))
626
627 ;; This is needed for some unknown reason to get type numbers right
628 (mapc #'find-type-dependencies type-list)
629
630 (let ((sorted-type-list
631 #+clisp (mapcar #'list type-list)
632 #-clisp
633 (sort-types-topologicaly
634 (mapcar
635 #'(lambda (type)
636 (cons type (find-type-dependencies type (type-options type))))
637 type-list))))
638 `(progn
639 ,@(mapcar
640 #'(lambda (pair)
641 (destructuring-bind (type . forward-p) pair
642 (expand-type-definition type forward-p (type-options type))))
643 sorted-type-list)
644 ,@(mapcar
645 #'(lambda (pair)
646 (destructuring-bind (type . forward-p) pair
647 (when forward-p
648 (expand-type-definition type nil (type-options type)))))
649 sorted-type-list)))))
650
651(defun expand-types-with-prefix (prefix args)
652 (expand-type-definitions (find-types prefix) args))
653
654(defun expand-types-in-library (system library args)
655 (let* ((filename (library-filename system library))
656 (types (loop
657 for (type-init . %filename) in *type-initializers*
658 when (equal filename %filename)
659 collect (funcall type-init))))
660 (expand-type-definitions types args)))
661
662(defun list-types-in-library (system library)
663 (let ((filename (library-filename system library)))
664 (loop
665 for (type-init . %filename) in *type-initializers*
666 when (equal filename %filename)
667 collect type-init)))
4812615b 668
3a935dfa 669(defmacro define-types-by-introspection (prefix &rest args)
7797f30a 670 (expand-types-with-prefix prefix args))
6556dccd 671
08cb5756 672(defexport define-types-by-introspection (prefix &rest args)
7797f30a 673 (list-autoexported-symbols (expand-types-with-prefix prefix args)))
674
675(defmacro define-types-in-library (system library &rest args)
676 (expand-types-in-library system library args))
677
678(defexport define-types-in-library (system library &rest args)
679 (list-autoexported-symbols (expand-types-in-library system library args)))
08cb5756 680
6556dccd 681
682;;;; Initialize all non static types in GObject
683
07dafdb0 684(init-types-in-library glib "libgobject-2.0")