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