1.0.22.20: Make a stab at having DEFTYPE types replace structure types.
[sbcl.git] / src / compiler / deftype.lisp
1 ;;;; This software is part of the SBCL system. See the README file for
2 ;;;; more information.
3 ;;;;
4 ;;;; This software is derived from the CMU CL system, which was
5 ;;;; written at Carnegie Mellon University and released into the
6 ;;;; public domain. The software is in the public domain and is
7 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
8 ;;;; files for more information.
9
10 (in-package "SB!IMPL")
11
12 (defun constant-type-expander (expansion)
13   (declare (optimize safety))
14   (lambda (whole)
15     (if (cdr whole)
16         (sb!kernel::arg-count-error 'deftype (car whole) (cdr whole) nil 0 0)
17         expansion)))
18
19 (defun %deftype (name)
20   (setf (classoid-cell-pcl-class (find-classoid-cell name :create t)) nil))
21
22 (def!macro sb!xc:deftype (name lambda-list &body body)
23   #!+sb-doc
24   "Define a new type, with syntax like DEFMACRO."
25   (unless (symbolp name)
26     (error "type name not a symbol: ~S" name))
27   (multiple-value-bind (expander-form doc source-location-form)
28       (multiple-value-bind (forms decls doc) (parse-body body)
29         ;; FIXME: We could use CONSTANTP here to deal with slightly more
30         ;; complex deftypes using CONSTANT-TYPE-EXPANDER, but that XC:CONSTANTP
31         ;; is not availble early enough.
32         (if (and (not lambda-list) (not decls) (not (cdr forms))
33                  (or (member (car forms) '(t nil))
34                      (eq 'quote (caar forms))))
35             (values `(constant-type-expander ,(car forms)) doc '(sb!c:source-location))
36             (with-unique-names (whole)
37               (multiple-value-bind (macro-body local-decs doc)
38                   (parse-defmacro lambda-list whole body name 'deftype :default-default ''*)
39                 (values `(lambda (,whole)
40                            ,@local-decs
41                            ,macro-body)
42                         doc
43                         nil)))))
44     `(progn
45        (eval-when (:compile-toplevel :load-toplevel :execute)
46          (%compiler-deftype ',name
47                             ',lambda-list
48                             ,expander-form
49                             ,doc
50                             ,source-location-form))
51        (eval-when (:load-toplevel :execute)
52          (%deftype ',name)))))