0.6.10.6:
[sbcl.git] / src / code / typedefs.lisp
1 ;;;; This file contains the definition of the CTYPE (Compiler TYPE)
2 ;;;; structure and related macros used for manipulating it. This is
3 ;;;; sort of a mini object system with rather odd dispatching rules.
4 ;;;; Other compile-time definitions needed by multiple files are also
5 ;;;; here.
6 ;;;;
7 ;;;; FIXME: The comment above about what's in this file is no longer so
8 ;;;; true now that I've split off type-class.lisp. Perhaps we should
9 ;;;; split off CTYPE into the same file as type-class.lisp, rename that
10 ;;;; file to ctype.lisp, move the current comment to the head of that file,
11 ;;;; and write a new comment for this file saying how this file holds
12 ;;;; concrete types.
13
14 ;;;; This software is part of the SBCL system. See the README file for
15 ;;;; more information.
16 ;;;;
17 ;;;; This software is derived from the CMU CL system, which was
18 ;;;; written at Carnegie Mellon University and released into the
19 ;;;; public domain. The software is in the public domain and is
20 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
21 ;;;; files for more information.
22
23 (in-package "SB!KERNEL")
24
25 (!begin-collecting-cold-init-forms)
26
27 ;;; Define the translation from a type-specifier to a type structure for
28 ;;; some particular type. Syntax is identical to DEFTYPE.
29 (defmacro !def-type-translator (name arglist &body body)
30   (check-type name symbol)
31   ;; FIXME: Now that the T%CL hack is ancient history and we just use CL
32   ;; instead, we can probably return to using PARSE-DEFMACRO here.
33   ;;
34   ;; was:
35   ;;   This song and dance more or less emulates PARSE-DEFMACRO. The reason for
36   ;;   doing this emulation instead of just calling PARSE-DEFMACRO is just that
37   ;;   at cross-compile time PARSE-DEFMACRO expects lambda-list keywords in the
38   ;;   T%CL package, which is not what we have here. Maybe there's a tidier
39   ;;   solution.. (Other than wishing that ANSI had used symbols in the KEYWORD
40   ;;   package as lambda list keywords, rather than using symbols in the LISP
41   ;;   package!)
42   (multiple-value-bind (whole wholeless-arglist)
43       (if (eq '&whole (car arglist))
44         (values (cadr arglist) (cddr arglist))
45         (values (gensym) arglist))
46     (multiple-value-bind (forms decls) (parse-body body nil)
47       `(progn
48          (!cold-init-forms
49           (setf (info :type :translator ',name)
50                 (lambda (,whole)
51                   (block ,name
52                     (destructuring-bind ,wholeless-arglist
53                         (rest ,whole) ; discarding NAME
54                       ,@decls
55                       ,@forms)))))
56          ',name))))
57
58 ;;; DEFVARs for these come later, after we have enough stuff defined.
59 (declaim (special *wild-type* *universal-type* *empty-type*))
60 \f
61 ;;; The XXX-Type structures include the CTYPE structure for some slots that
62 ;;; apply to all types.
63 (def!struct (ctype (:conc-name type-)
64                    (:constructor nil)
65                    (:make-load-form-fun make-type-load-form)
66                    #-sb-xc-host (:pure t))
67   ;; The class of this type.
68   ;;
69   ;; FIXME: It's unnecessarily confusing to have a structure accessor
70   ;; named TYPE-CLASS-INFO which is an accessor for the CTYPE structure
71   ;; even though the TYPE-CLASS structure also exists in the system.
72   ;; Rename this slot: TYPE-CLASS or ASSOCIATED-TYPE-CLASS or something.
73   (class-info (required-argument) :type type-class)
74   ;; True if this type has a fixed number of members, and as such could
75   ;; possibly be completely specified in a MEMBER type. This is used by the
76   ;; MEMBER type methods.
77   (enumerable nil :type (member t nil) :read-only t)
78   ;; an arbitrary hash code used in EQ-style hashing of identity (since EQ
79   ;; hashing can't be done portably)
80   (hash-value (random (1+ most-positive-fixnum))
81               :type (and fixnum unsigned-byte)
82               :read-only t))
83 (def!method print-object ((ctype ctype) stream)
84   (print-unreadable-object (ctype stream :type t)
85     (prin1 (type-specifier ctype) stream)))
86
87 ;;; Just dump it as a specifier. (We'll convert it back upon loading.)
88 (defun make-type-load-form (type)
89   (declare (type ctype type))
90   `(specifier-type ',(type-specifier type)))
91 \f
92 ;;;; utilities
93
94 ;;; Like ANY and EVERY, except that we handle two-arg uncertain predicates.
95 ;;; If the result is uncertain, then we return Default from the block PUNT.
96 ;;; If LIST-FIRST is true, then the list element is the first arg, otherwise
97 ;;; the second.
98 (defmacro any-type-op (op thing list &key (default '(values nil nil))
99                           list-first)
100   (let ((n-this (gensym))
101         (n-thing (gensym))
102         (n-val (gensym))
103         (n-win (gensym))
104         (n-uncertain (gensym)))
105     `(let ((,n-thing ,thing)
106            (,n-uncertain nil))
107        (dolist (,n-this ,list
108                         (if ,n-uncertain
109                             (return-from punt-type-method ,default)
110                             nil))
111          (multiple-value-bind (,n-val ,n-win)
112              ,(if list-first
113                   `(,op ,n-this ,n-thing)
114                 `(,op ,n-thing ,n-this))
115            (unless ,n-win (setq ,n-uncertain t))
116            (when ,n-val (return t)))))))
117 (defmacro every-type-op (op thing list &key (default '(values nil nil))
118                             list-first)
119   (let ((n-this (gensym))
120         (n-thing (gensym))
121         (n-val (gensym))
122         (n-win (gensym)))
123     `(let ((,n-thing ,thing))
124        (dolist (,n-this ,list t)
125          (multiple-value-bind (,n-val ,n-win)
126              ,(if list-first
127                   `(,op ,n-this ,n-thing)
128                 `(,op ,n-thing ,n-this))
129            (unless ,n-win (return-from punt-type-method ,default))
130            (unless ,n-val (return nil)))))))
131
132 ;;; Compute the intersection for types that intersect only when one is a
133 ;;; hierarchical subtype of the other.
134 (defun vanilla-intersection (type1 type2)
135   (multiple-value-bind (stp1 win1) (csubtypep type1 type2)
136     (multiple-value-bind (stp2 win2) (csubtypep type2 type1)
137       (cond (stp1 (values type1 t))
138             (stp2 (values type2 t))
139             ((and win1 win2) (values *empty-type* t))
140             (t
141              (values type1 nil))))))
142
143 (defun vanilla-union (type1 type2)
144   (cond ((csubtypep type1 type2) type2)
145         ((csubtypep type2 type1) type1)
146         (t nil)))
147
148 ;;; Hash two things (types) down to 8 bits. In CMU CL this was an EQ hash, but
149 ;;; since it now needs to run in vanilla ANSI Common Lisp at cross-compile
150 ;;; time, it's now based on the CTYPE-HASH-VALUE field instead.
151 ;;;
152 ;;; FIXME: This was a macro in CMU CL, and is now an INLINE function. Is
153 ;;; it important for it to be INLINE, or could be become an ordinary
154 ;;; function without significant loss? -- WHN 19990413
155 #!-sb-fluid (declaim (inline type-cache-hash))
156 (declaim (ftype (function (ctype ctype) (unsigned-byte 8)) type-cache-hash))
157 (defun type-cache-hash (type1 type2)
158   (logand (logxor (ash (type-hash-value type1) -3)
159                   (type-hash-value type2))
160           #xFF))
161 \f
162 ;;;; cold loading initializations
163
164 (!defun-from-collected-cold-init-forms !typedefs-cold-init)