1 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; This software is derived from software originally released by Xerox
5 ;;;; Corporation. Copyright and release statements follow. Later modifications
6 ;;;; to the software are in the public domain and are provided with
7 ;;;; absolutely no warranty. See the COPYING and CREDITS files for more
10 ;;;; copyright information from original PCL sources:
12 ;;;; Copyright (c) 1985, 1986, 1987, 1988, 1989, 1990 Xerox Corporation.
13 ;;;; All rights reserved.
15 ;;;; Use and copying of this software and preparation of derivative works based
16 ;;;; upon this software are permitted. Any distribution of this software or
17 ;;;; derivative works must comply with all applicable United States export
20 ;;;; This software is made available AS IS, and Xerox Corporation makes no
21 ;;;; warranty about the software, its performance or its conformity to any
26 ;;; (These are left over from the days when PCL was an add-on package
27 ;;; for a pre-CLOS Common Lisp. They shouldn't happen in a normal
28 ;;; build, of course, but they might happen if someone is experimenting
29 ;;; and debugging, and it's probably worth complaining if they do,
30 ;;; so we've left 'em in.)
31 (when (eq *boot-state* 'complete)
32 (error "Trying to load (or compile) PCL in an environment in which it~%~
33 has already been loaded. This doesn't work, you will have to~%~
34 get a fresh lisp (reboot) and then load PCL."))
36 (cerror "Try loading (or compiling) PCL anyways."
37 "Trying to load (or compile) PCL in an environment in which it~%~
38 has already been partially loaded. This may not work, you may~%~
39 need to get a fresh lisp (reboot) and then load PCL."))
41 ;;; comments from CMU CL version of PCL:
42 ;;; This is like fdefinition on the Lispm. If Common Lisp had
43 ;;; something like function specs I wouldn't need this. On the other
44 ;;; hand, I don't like the way this really works so maybe function
45 ;;; specs aren't really right either?
46 ;;; I also don't understand the real implications of a Lisp-1 on this
47 ;;; sort of thing. Certainly some of the lossage in all of this is
48 ;;; because these SPECs name global definitions.
49 ;;; Note that this implementation is set up so that an implementation
50 ;;; which has a 'real' function spec mechanism can use that instead
51 ;;; and in that way get rid of setf generic function names.
52 (defmacro parse-gspec (spec
53 (non-setf-var . non-setf-case))
54 `(let ((,non-setf-var ,spec)) ,@non-setf-case))
56 ;;; If symbol names a function which is traced, return the untraced
57 ;;; definition. This lets us get at the generic function object even
58 ;;; when it is traced.
59 (defun unencapsulated-fdefinition (symbol)
62 ;;; If symbol names a function which is traced, redefine the `real'
63 ;;; definition without affecting the trace.
64 (defun fdefine-carefully (name new-definition)
66 (sb-c::note-name-defined name :function)
68 (setf (fdefinition name) new-definition))
72 (name (fboundp name))))
74 (defun gmakunbound (spec)
76 (name (fmakunbound name))))
78 (defun gdefinition (spec)
80 (name (unencapsulated-fdefinition name))))
82 (defun (setf gdefinition) (new-value spec)
84 (name (fdefine-carefully name new-value))))
86 (declaim (special *the-class-t*
87 *the-class-vector* *the-class-symbol*
88 *the-class-string* *the-class-sequence*
89 *the-class-rational* *the-class-ratio*
90 *the-class-number* *the-class-null* *the-class-list*
91 *the-class-integer* *the-class-float* *the-class-cons*
92 *the-class-complex* *the-class-character*
93 *the-class-bit-vector* *the-class-array*
96 *the-class-slot-object*
97 *the-class-structure-object*
98 *the-class-std-object*
99 *the-class-standard-object*
100 *the-class-funcallable-standard-object*
102 *the-class-generic-function*
103 *the-class-built-in-class*
104 *the-class-slot-class*
105 *the-class-structure-class*
106 *the-class-std-class*
107 *the-class-standard-class*
108 *the-class-funcallable-standard-class*
110 *the-class-standard-method*
111 *the-class-standard-reader-method*
112 *the-class-standard-writer-method*
113 *the-class-standard-boundp-method*
114 *the-class-standard-generic-function*
115 *the-class-standard-effective-slot-definition*
117 *the-eslotd-standard-class-slots*
118 *the-eslotd-funcallable-standard-class-slots*))
120 (declaim (special *the-wrapper-of-t*
121 *the-wrapper-of-vector* *the-wrapper-of-symbol*
122 *the-wrapper-of-string* *the-wrapper-of-sequence*
123 *the-wrapper-of-rational* *the-wrapper-of-ratio*
124 *the-wrapper-of-number* *the-wrapper-of-null*
125 *the-wrapper-of-list* *the-wrapper-of-integer*
126 *the-wrapper-of-float* *the-wrapper-of-cons*
127 *the-wrapper-of-complex* *the-wrapper-of-character*
128 *the-wrapper-of-bit-vector* *the-wrapper-of-array*))
130 ;;;; type specifier hackery
132 ;;; internal to this file
133 (defun coerce-to-class (class &optional make-forward-referenced-class-p)
135 (or (find-class class (not make-forward-referenced-class-p))
136 (ensure-class class))
140 (defun specializer-from-type (type &aux args)
142 (setq args (cdr type) type (car type)))
143 (cond ((symbolp type)
144 (or (and (null args) (find-class type))
146 (class (coerce-to-class (car args)))
147 (prototype (make-instance 'class-prototype-specializer
148 :object (coerce-to-class (car args))))
149 (class-eq (class-eq-specializer (coerce-to-class (car args))))
150 (eql (intern-eql-specializer (car args))))))
151 ((and (null args) (typep type 'cl:class))
152 (or (sb-kernel:class-pcl-class type)
153 (find-structure-class (cl:class-name type))))
154 ((specializerp type) type)))
157 (defun type-from-specializer (specl)
161 (unless (member (car specl) '(class prototype class-eq eql))
162 (error "~S is not a legal specializer type." specl))
165 (when (symbolp specl)
166 ;;maybe (or (find-class specl nil) (ensure-class specl)) instead?
167 (setq specl (find-class specl)))
168 (or (not (eq *boot-state* 'complete))
169 (specializerp specl)))
170 (specializer-type specl))
172 (error "~S is neither a type nor a specializer." specl))))
174 (defun type-class (type)
175 (declare (special *the-class-t*))
176 (setq type (type-from-specializer type))
180 (error "bad argument to TYPE-CLASS"))
182 (eql (class-of (cadr type)))
183 (prototype (class-of (cadr type))) ;?
184 (class-eq (cadr type))
185 (class (cadr type)))))
187 (defun class-eq-type (class)
188 (specializer-type (class-eq-specializer class)))
190 ;;; internal to this file..
192 ;;; These functions are a pale imitation of their namesake. They accept
193 ;;; class objects or types where they should.
194 (defun *normalize-type (type)
196 (if (member (car type) '(not and or))
197 `(,(car type) ,@(mapcar #'*normalize-type (cdr type)))
198 (if (null (cdr type))
199 (*normalize-type (car type))
202 (let ((class (find-class type nil)))
204 (let ((type (specializer-type class)))
205 (if (listp type) type `(,type)))
207 ((or (not (eq *boot-state* 'complete))
209 (specializer-type type))
211 (error "~S is not a type." type))))
213 ;;; internal to this file...
214 (defun convert-to-system-type (type)
216 ((not and or) `(,(car type) ,@(mapcar #'convert-to-system-type
218 ((class class-eq) ; class-eq is impossible to do right
219 (sb-kernel:layout-class (class-wrapper (cadr type))))
221 (t (if (null (cdr type))
225 ;;; Writing the missing NOT and AND clauses will improve the quality
226 ;;; of code generated by GENERATE-DISCRIMINATION-NET, but calling
227 ;;; SUBTYPEP in place of just returning (VALUES NIL NIL) can be very
228 ;;; slow. *SUBTYPEP is used by PCL itself, and must be fast.
230 ;;; FIXME: SB-KERNEL has fast-and-not-quite-precise type code for use
231 ;;; in the compiler. Could we share some of it here?
232 (defun *subtypep (type1 type2)
233 (if (equal type1 type2)
235 (if (eq *boot-state* 'early)
236 (values (eq type1 type2) t)
237 (let ((*in-precompute-effective-methods-p* t))
238 (declare (special *in-precompute-effective-methods-p*))
239 ;; FIXME: *IN-PRECOMPUTE-EFFECTIVE-METHODS-P* is not a
240 ;; good name. It changes the way
241 ;; CLASS-APPLICABLE-USING-CLASS-P works.
242 (setq type1 (*normalize-type type1))
243 (setq type2 (*normalize-type type2))
246 (values nil nil)) ; XXX We should improve this.
248 (values nil nil)) ; XXX We should improve this.
249 ((eql wrapper-eq class-eq class)
250 (multiple-value-bind (app-p maybe-app-p)
251 (specializer-applicable-using-type-p type2 type1)
252 (values app-p (or app-p (not maybe-app-p)))))
254 (subtypep (convert-to-system-type type1)
255 (convert-to-system-type type2))))))))
257 (defvar *built-in-class-symbols* ())
258 (defvar *built-in-wrapper-symbols* ())
260 (defun get-built-in-class-symbol (class-name)
261 (or (cadr (assq class-name *built-in-class-symbols*))
262 (let ((symbol (intern (format nil
264 (symbol-name class-name))
266 (push (list class-name symbol) *built-in-class-symbols*)
269 (defun get-built-in-wrapper-symbol (class-name)
270 (or (cadr (assq class-name *built-in-wrapper-symbols*))
271 (let ((symbol (intern (format nil
272 "*THE-WRAPPER-OF-~A*"
273 (symbol-name class-name))
275 (push (list class-name symbol) *built-in-wrapper-symbols*)
278 (pushnew '%class *var-declarations*)
279 (pushnew '%variable-rebinding *var-declarations*)
281 (defun variable-class (var env)
282 (caddr (var-declaration 'class var env)))
284 (defvar *name->class->slotd-table* (make-hash-table))
286 (defvar *standard-method-combination*)
288 (defun make-class-predicate-name (name)
289 (intern (format nil "~A::~A class predicate"
290 (package-name (symbol-package name))
294 (defun plist-value (object name)
295 (getf (object-plist object) name))
297 (defun (setf plist-value) (new-value object name)
299 (setf (getf (object-plist object) name) new-value)
301 (remf (object-plist object) name)
304 ;;;; built-in classes
306 ;;; FIXME: This was the portable PCL way of setting up
307 ;;; *BUILT-IN-CLASSES*, but in SBCL (as in CMU CL) it's almost
308 ;;; entirely wasted motion, since it's immediately overwritten by a
309 ;;; result mostly derived from SB-KERNEL::*BUILT-IN-CLASSES*. However,
310 ;;; we can't just delete it, since the fifth element from each entry
311 ;;; (a prototype of the class) is still in the final result. It would
312 ;;; be nice to clean this up so that the other, never-used stuff is
313 ;;; gone, perhaps finding a tidier way to represent examples of each
316 ;;; FIXME: This can probably be blown away after bootstrapping.
317 ;;; And SB-KERNEL::*BUILT-IN-CLASSES*, too..
319 (defvar *built-in-classes*
320 ;; name supers subs cdr of cpl
322 '(;(t () (number sequence array character symbol) ())
323 (number (t) (complex float rational) (t))
324 (complex (number) () (number t)
326 (float (number) () (number t)
328 (rational (number) (integer ratio) (number t))
329 (integer (rational) () (rational number t)
331 (ratio (rational) () (rational number t)
334 (sequence (t) (list vector) (t))
335 (list (sequence) (cons null) (sequence t))
336 (cons (list) () (list sequence t)
339 (array (t) (vector) (t)
342 sequence) (string bit-vector) (array sequence t)
344 (string (vector) () (vector array sequence t)
346 (bit-vector (vector) () (vector array sequence t)
348 (character (t) () (t)
351 (symbol (t) (null) (t)
354 list) () (symbol list sequence t)
358 ;;; Grovel over SB-KERNEL::*BUILT-IN-CLASSES* in order to set
359 ;;; SB-PCL:*BUILT-IN-CLASSES*.
360 (/show "about to set up SB-PCL::*BUILT-IN-CLASSES*")
361 (defvar *built-in-classes*
362 (labels ((direct-supers (class)
363 (/noshow "entering DIRECT-SUPERS" (sb-kernel::class-name class))
364 (if (typep class 'cl:built-in-class)
365 (sb-kernel:built-in-class-direct-superclasses class)
366 (let ((inherits (sb-kernel:layout-inherits
367 (sb-kernel:class-layout class))))
369 (list (svref inherits (1- (length inherits)))))))
371 (/noshow "entering DIRECT-SUBS" (sb-kernel::class-name class))
373 (let ((subs (sb-kernel:class-subclasses class)))
379 (when (member class (direct-supers sub))
382 (prototype (class-name)
383 (let ((assoc (assoc class-name
384 '((complex . #c(1 1))
400 ;; This is the default prototype value which was
401 ;; used, without explanation, by the CMU CL code
402 ;; we're derived from. Evidently it's safe in all
405 (mapcar (lambda (kernel-bic-entry)
406 (/noshow "setting up" kernel-bic-entry)
407 (let* ((name (car kernel-bic-entry))
408 (class (cl:find-class name)))
411 ,(mapcar #'cl:class-name (direct-supers class))
412 ,(mapcar #'cl:class-name (direct-subs class))
415 (cl:class-name (sb-kernel:layout-class x)))
417 (sb-kernel:layout-inherits
418 (sb-kernel:class-layout class))))
420 (remove-if (lambda (kernel-bic-entry)
421 (member (first kernel-bic-entry)
422 ;; I'm not sure why these are removed from
423 ;; the list, but that's what the original
424 ;; CMU CL code did. -- WHN 20000715
425 '(t sb-kernel:instance
426 sb-kernel:funcallable-instance
428 sb-kernel::*built-in-classes*))))
429 (/noshow "done setting up SB-PCL::*BUILT-IN-CLASSES*")
431 ;;;; the classes that define the kernel of the metabraid
434 (:metaclass built-in-class))
436 (defclass sb-kernel:instance (t) ()
437 (:metaclass built-in-class))
439 (defclass function (t) ()
440 (:metaclass built-in-class))
442 (defclass sb-kernel:funcallable-instance (function) ()
443 (:metaclass built-in-class))
445 (defclass stream (sb-kernel:instance) ()
446 (:metaclass built-in-class))
448 (defclass slot-object (t) ()
449 (:metaclass slot-class))
451 (defclass structure-object (slot-object sb-kernel:instance) ()
452 (:metaclass structure-class))
454 (defstruct (dead-beef-structure-object
455 (:constructor |STRUCTURE-OBJECT class constructor|)
458 (defclass std-object (slot-object) ()
459 (:metaclass std-class))
461 (defclass standard-object (std-object sb-kernel:instance) ())
463 (defclass funcallable-standard-object (std-object
464 sb-kernel:funcallable-instance)
466 (:metaclass funcallable-standard-class))
468 (defclass specializer (standard-object)
471 :reader specializer-type)))
473 (defclass definition-source-mixin (std-object)
475 :initform *load-pathname*
476 :reader definition-source
477 :initarg :definition-source))
478 (:metaclass std-class))
480 (defclass plist-mixin (std-object)
483 :accessor object-plist))
484 (:metaclass std-class))
486 (defclass documentation-mixin (plist-mixin)
488 (:metaclass std-class))
490 (defclass dependent-update-mixin (plist-mixin)
492 (:metaclass std-class))
494 ;;; The class CLASS is a specified basic class. It is the common
495 ;;; superclass of any kind of class. That is, any class that can be a
496 ;;; metaclass must have the class CLASS in its class precedence list.
497 (defclass class (documentation-mixin
498 dependent-update-mixin
499 definition-source-mixin
504 :accessor class-name)
505 (class-eq-specializer
507 :reader class-eq-specializer)
510 :reader class-direct-superclasses)
511 ;; Note: The (CLASS-)DIRECT-SUBCLASSES for STRUCTURE-CLASSes and
512 ;; CONDITION-CLASSes are lazily computed whenever the subclass info
513 ;; becomes available, i.e. when the PCL class is created.
516 :reader class-direct-subclasses)
518 :initform (cons nil nil))
521 :reader class-predicate-name)))
523 ;;; The class PCL-CLASS is an implementation-specific common
524 ;;; superclass of all specified subclasses of the class CLASS.
525 (defclass pcl-class (class)
526 ((class-precedence-list
527 :reader class-precedence-list)
530 :reader class-can-precede-list)
531 (incompatible-superclass-list
533 :accessor class-incompatible-superclass-list)
536 :reader class-wrapper)
539 :reader class-prototype)))
541 (defclass slot-class (pcl-class)
544 :accessor class-direct-slots)
547 :accessor class-slots)
550 :accessor class-initialize-info)))
552 ;;; The class STD-CLASS is an implementation-specific common
553 ;;; superclass of the classes STANDARD-CLASS and
554 ;;; FUNCALLABLE-STANDARD-CLASS.
555 (defclass std-class (slot-class)
558 (defclass standard-class (std-class)
561 (defclass funcallable-standard-class (std-class)
564 (defclass forward-referenced-class (pcl-class) ())
566 (defclass built-in-class (pcl-class) ())
568 (defclass structure-class (slot-class)
571 :accessor class-defstruct-form)
572 (defstruct-constructor
574 :accessor class-defstruct-constructor)
577 :initarg :from-defclass-p)))
579 (defclass specializer-with-object (specializer) ())
581 (defclass exact-class-specializer (specializer) ())
583 (defclass class-eq-specializer (exact-class-specializer
584 specializer-with-object)
585 ((object :initarg :class
586 :reader specializer-class
587 :reader specializer-object)))
589 (defclass class-prototype-specializer (specializer-with-object)
590 ((object :initarg :class
591 :reader specializer-class
592 :reader specializer-object)))
594 (defclass eql-specializer (exact-class-specializer specializer-with-object)
595 ((object :initarg :object :reader specializer-object
596 :reader eql-specializer-object)))
598 (defvar *eql-specializer-table* (make-hash-table :test 'eql))
600 (defun intern-eql-specializer (object)
601 (or (gethash object *eql-specializer-table*)
602 (setf (gethash object *eql-specializer-table*)
603 (make-instance 'eql-specializer :object object))))
605 ;;;; slot definitions
607 (defclass slot-definition (standard-object)
611 :accessor slot-definition-name)
615 :accessor slot-definition-initform)
618 :initarg :initfunction
619 :accessor slot-definition-initfunction)
623 :accessor slot-definition-readers)
627 :accessor slot-definition-writers)
631 :accessor slot-definition-initargs)
635 :accessor slot-definition-type)
638 :initarg :documentation)
642 :accessor slot-definition-class)))
644 (defclass standard-slot-definition (slot-definition)
648 :accessor slot-definition-allocation)
651 :initarg :allocation-class
652 :accessor slot-definition-allocation-class)))
654 (defclass structure-slot-definition (slot-definition)
655 ((defstruct-accessor-symbol
657 :initarg :defstruct-accessor-symbol
658 :accessor slot-definition-defstruct-accessor-symbol)
659 (internal-reader-function
661 :initarg :internal-reader-function
662 :accessor slot-definition-internal-reader-function)
663 (internal-writer-function
665 :initarg :internal-writer-function
666 :accessor slot-definition-internal-writer-function)))
668 (defclass direct-slot-definition (slot-definition)
671 (defclass effective-slot-definition (slot-definition)
672 ((reader-function ; (lambda (object) ...)
673 :accessor slot-definition-reader-function)
674 (writer-function ; (lambda (new-value object) ...)
675 :accessor slot-definition-writer-function)
676 (boundp-function ; (lambda (object) ...)
677 :accessor slot-definition-boundp-function)
681 (defclass standard-direct-slot-definition (standard-slot-definition
682 direct-slot-definition)
685 (defclass standard-effective-slot-definition (standard-slot-definition
686 effective-slot-definition)
687 ((location ; nil, a fixnum, a cons: (slot-name . value)
689 :accessor slot-definition-location)))
691 (defclass structure-direct-slot-definition (structure-slot-definition
692 direct-slot-definition)
695 (defclass structure-effective-slot-definition (structure-slot-definition
696 effective-slot-definition)
699 (defclass method (standard-object) ())
701 (defclass standard-method (definition-source-mixin plist-mixin method)
704 :accessor method-generic-function)
707 ;;; :initarg :qualifiers
708 ;;; :reader method-qualifiers)
711 :initarg :specializers
712 :reader method-specializers)
715 :initarg :lambda-list
716 :reader method-lambda-list)
719 :initarg :function) ;no writer
722 :initarg :fast-function ;no writer
723 :reader method-fast-function)
726 ;;; :initarg :documentation
727 ;;; :reader method-documentation)
730 (defclass standard-accessor-method (standard-method)
731 ((slot-name :initform nil
733 :reader accessor-method-slot-name)
734 (slot-definition :initform nil
735 :initarg :slot-definition
736 :reader accessor-method-slot-definition)))
738 (defclass standard-reader-method (standard-accessor-method) ())
740 (defclass standard-writer-method (standard-accessor-method) ())
742 (defclass standard-boundp-method (standard-accessor-method) ())
744 (defclass generic-function (dependent-update-mixin
745 definition-source-mixin
747 funcallable-standard-object)
748 (;; We need to make a distinction between the methods initially set
749 ;; up by :METHOD options to DEFGENERIC and the ones set up later by
750 ;; DEFMETHOD, because ANSI's specifies that executing DEFGENERIC on
751 ;; an already-DEFGENERICed function clears the methods set by the
752 ;; previous DEFGENERIC, but not methods set by DEFMETHOD. (Making
753 ;; this distinction seems a little kludgy, but it has the positive
754 ;; effect of making it so that loading a file a.lisp containing
755 ;; DEFGENERIC, then loading a second file b.lisp containing
756 ;; DEFMETHOD, then modifying and reloading a.lisp and/or b.lisp
757 ;; tends to leave the generic function in a state consistent with
758 ;; the most-recently-loaded state of a.lisp and b.lisp.)
761 :accessor generic-function-initial-methods))
762 (:metaclass funcallable-standard-class))
764 (defclass standard-generic-function (generic-function)
768 :accessor generic-function-name)
771 :accessor generic-function-methods
774 :initarg :method-class
775 :accessor generic-function-method-class)
777 :initarg :method-combination
778 :accessor generic-function-method-combination)
780 :initarg :declarations
782 :accessor generic-function-declarations)
784 :initform (make-arg-info)
788 :accessor gf-dfun-state))
789 (:metaclass funcallable-standard-class)
790 (:default-initargs :method-class *the-class-standard-method*
791 :method-combination *standard-method-combination*))
793 (defclass method-combination (standard-object) ())
795 (defclass standard-method-combination (definition-source-mixin
798 :reader method-combination-type
801 :reader method-combination-documentation
802 :initarg :documentation)
804 :reader method-combination-options
807 (defclass long-method-combination (standard-method-combination)
810 :reader long-method-combination-function)
812 :initarg :args-lambda-list
813 :reader long-method-combination-args-lambda-list)))
815 (defparameter *early-class-predicates*
816 '((specializer specializerp)
817 (exact-class-specializer exact-class-specializer-p)
818 (class-eq-specializer class-eq-specializer-p)
819 (eql-specializer eql-specializer-p)
821 (slot-class slot-class-p)
822 (std-class std-class-p)
823 (standard-class standard-class-p)
824 (funcallable-standard-class funcallable-standard-class-p)
825 (structure-class structure-class-p)
826 (forward-referenced-class forward-referenced-class-p)
828 (standard-method standard-method-p)
829 (standard-accessor-method standard-accessor-method-p)
830 (standard-reader-method standard-reader-method-p)
831 (standard-writer-method standard-writer-method-p)
832 (standard-boundp-method standard-boundp-method-p)
833 (generic-function generic-function-p)
834 (standard-generic-function standard-generic-function-p)
835 (method-combination method-combination-p)
836 (long-method-combination long-method-combination-p)))