1.0.27.32: implement and use SB!XC:GENSYM
[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   (declare (type symbol name))
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 (sb!xc:gensym) arglist))
46     (multiple-value-bind (forms decls)
47         (parse-body body :doc-string-allowed nil)
48       `(progn
49          (!cold-init-forms
50           (setf (info :type :translator ',name)
51                 (lambda (,whole)
52                   (block ,name
53                     (destructuring-bind ,wholeless-arglist
54                         (rest ,whole) ; discarding NAME
55                       ,@decls
56                       ,@forms)))))
57          ',name))))
58
59 ;;; DEFVARs for these come later, after we have enough stuff defined.
60 (declaim (special *wild-type* *universal-type* *empty-type*))
61 \f
62 (defvar *type-random-state*)
63
64 ;;; the base class for the internal representation of types
65 (def!struct (ctype (:conc-name type-)
66                    (:constructor nil)
67                    (:make-load-form-fun make-type-load-form)
68                    #-sb-xc-host (:pure t))
69   ;; the class of this type
70   ;;
71   ;; FIXME: It's unnecessarily confusing to have a structure accessor
72   ;; named TYPE-CLASS-INFO which is an accessor for the CTYPE structure
73   ;; even though the TYPE-CLASS structure also exists in the system.
74   ;; Rename this slot: TYPE-CLASS or ASSOCIATED-TYPE-CLASS or something.
75   (class-info (missing-arg) :type type-class)
76   ;; True if this type has a fixed number of members, and as such
77   ;; could possibly be completely specified in a MEMBER type. This is
78   ;; used by the MEMBER type methods.
79   (enumerable nil :read-only t)
80   ;; an arbitrary hash code used in EQ-style hashing of identity
81   ;; (since EQ hashing can't be done portably)
82   (hash-value (random #.(ash 1 15)
83                       (if (boundp '*type-random-state*)
84                           *type-random-state*
85                           (setf *type-random-state*
86                                 (make-random-state))))
87               :type (and fixnum unsigned-byte)
88               :read-only t)
89   ;; Can this object contain other types? A global property of our
90   ;; implementation (which unfortunately seems impossible to enforce
91   ;; with assertions or other in-the-code checks and constraints) is
92   ;; that subclasses which don't contain other types correspond to
93   ;; disjoint subsets (except of course for the NAMED-TYPE T, which
94   ;; covers everything). So NUMBER-TYPE is disjoint from CONS-TYPE is
95   ;; is disjoint from MEMBER-TYPE and so forth. But types which can
96   ;; contain other types, like HAIRY-TYPE and INTERSECTION-TYPE, can
97   ;; violate this rule.
98   (might-contain-other-types-p nil :read-only t))
99 (def!method print-object ((ctype ctype) stream)
100   (print-unreadable-object (ctype stream :type t)
101     (prin1 (type-specifier ctype) stream)))
102
103 ;;; Just dump it as a specifier. (We'll convert it back upon loading.)
104 (defun make-type-load-form (type)
105   (declare (type ctype type))
106   `(specifier-type ',(type-specifier type)))
107 \f
108 ;;;; miscellany
109
110 ;;; Look for nice relationships for types that have nice relationships
111 ;;; only when one is a hierarchical subtype of the other.
112 (defun hierarchical-intersection2 (type1 type2)
113   (multiple-value-bind (subtypep1 win1) (csubtypep type1 type2)
114     (multiple-value-bind (subtypep2 win2) (csubtypep type2 type1)
115       (cond (subtypep1 type1)
116             (subtypep2 type2)
117             ((and win1 win2) *empty-type*)
118             (t nil)))))
119 (defun hierarchical-union2 (type1 type2)
120   (cond ((csubtypep type1 type2) type2)
121         ((csubtypep type2 type1) type1)
122         (t nil)))
123
124 ;;; Hash two things (types) down to 8 bits. In CMU CL this was an EQ
125 ;;; hash, but since it now needs to run in vanilla ANSI Common Lisp at
126 ;;; cross-compile time, it's now based on the CTYPE-HASH-VALUE field
127 ;;; instead.
128 ;;;
129 ;;; FIXME: This was a macro in CMU CL, and is now an INLINE function. Is
130 ;;; it important for it to be INLINE, or could be become an ordinary
131 ;;; function without significant loss? -- WHN 19990413
132 #!-sb-fluid (declaim (inline type-cache-hash))
133 (declaim (ftype (function (ctype ctype) (unsigned-byte 8)) type-cache-hash))
134 (defun type-cache-hash (type1 type2)
135   (logand (logxor (ash (type-hash-value type1) -3)
136                   (type-hash-value type2))
137           #xFF))
138 #!-sb-fluid (declaim (inline type-list-cache-hash))
139 (declaim (ftype (function (list) (unsigned-byte 8)) type-list-cache-hash))
140 (defun type-list-cache-hash (types)
141   (logand (loop with res = 0
142              for type in types
143              for hash = (type-hash-value type)
144              do (setq res (logxor res hash))
145              finally (return res))
146           #xFF))
147 \f
148 ;;;; cold loading initializations
149
150 (!defun-from-collected-cold-init-forms !typedefs-cold-init)