d041a83f4aabb8785c8179ea48e3f06dd274759f
[sbcl.git] / src / code / alien-type.lisp
1 ;;;; ALIEN-related type system stuff, done later
2 ;;;; than other type system stuff because it depends on the definition
3 ;;;; of the ALIEN-VALUE target structure type
4
5 ;;;; This software is part of the SBCL system. See the README file for
6 ;;;; more information.
7 ;;;;
8 ;;;; This software is derived from the CMU CL system, which was
9 ;;;; written at Carnegie Mellon University and released into the
10 ;;;; public domain. The software is in the public domain and is
11 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
12 ;;;; files for more information.
13
14 (in-package "SB!KERNEL")
15
16 (!begin-collecting-cold-init-forms)
17
18 (defstruct (alien-type-type
19             (:include ctype
20                       (class-info (type-class-or-lose 'alien)))
21             (:constructor %make-alien-type-type (alien-type))
22             (:copier nil))
23   (alien-type nil :type alien-type))
24
25 (!define-type-class alien)
26
27 (!define-type-method (alien :unparse) (type)
28   `(alien ,(unparse-alien-type (alien-type-type-alien-type type))))
29
30 (!define-type-method (alien :simple-subtypep) (type1 type2)
31   (values (alien-subtype-p (alien-type-type-alien-type type1)
32                            (alien-type-type-alien-type type2))
33           t))
34
35 ;;; KLUDGE: This !DEFINE-SUPERCLASSES gets executed much later than the
36 ;;; others (toplevel form time instead of cold load init time) because
37 ;;; ALIEN-VALUE itself is a structure which isn't defined until fairly
38 ;;; late.
39 ;;;
40 ;;; FIXME: I'm somewhat tempted to just punt ALIEN from the type system.
41 ;;; It's sufficiently unlike the others that it's a bit of a pain, and
42 ;;; it doesn't seem to be put to any good use either in type inference or
43 ;;; in type declarations.
44 (!define-superclasses alien ((alien-value)) progn)
45
46 (!define-type-method (alien :simple-=) (type1 type2)
47   (let ((alien-type-1 (alien-type-type-alien-type type1))
48         (alien-type-2 (alien-type-type-alien-type type2)))
49     (values (or (eq alien-type-1 alien-type-2)
50                 (alien-type-= alien-type-1 alien-type-2))
51             t)))
52
53 (!def-type-translator alien (&optional (alien-type nil))
54   (typecase alien-type
55     (null
56      (make-alien-type-type))
57     (alien-type
58      (make-alien-type-type alien-type))
59     (t
60      (make-alien-type-type (parse-alien-type alien-type (make-null-lexenv))))))
61
62 (defun make-alien-type-type (&optional alien-type)
63   (if alien-type
64       (let ((lisp-rep-type (compute-lisp-rep-type alien-type)))
65         (if lisp-rep-type
66             (specifier-type lisp-rep-type)
67             (%make-alien-type-type alien-type)))
68       *universal-type*))
69
70 (!defun-from-collected-cold-init-forms !alien-type-cold-init)