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 #-sb-fluid (declaim (inline gdefinition))
42 (defun gdefinition (spec)
43 ;; This is null layer right now, but once FDEFINITION stops bypasssing
44 ;; fwrappers/encapsulations we can do that here.
47 (defun (setf gdefinition) (new-value spec)
48 ;; This is almost a null layer right now, but once (SETF
49 ;; FDEFINITION) stops bypasssing fwrappers/encapsulations we can do
51 (sb-c::note-name-defined spec :function) ; FIXME: do we need this? Why?
52 (setf (fdefinition spec) new-value))
54 ;;;; type specifier hackery
56 ;;; internal to this file
57 (defun coerce-to-class (class &optional make-forward-referenced-class-p)
59 (or (find-class class (not make-forward-referenced-class-p))
64 (defun specializer-from-type (type &aux args)
66 (setq args (cdr type) type (car type)))
68 (or (and (null args) (find-class type))
70 (class (coerce-to-class (car args)))
71 (prototype (make-instance 'class-prototype-specializer
72 :object (coerce-to-class (car args))))
73 (class-eq (class-eq-specializer (coerce-to-class (car args))))
74 (eql (intern-eql-specializer (car args))))))
75 ;; FIXME: do we still need this?
76 ((and (null args) (typep type 'classoid))
77 (or (classoid-pcl-class type)
78 (ensure-non-standard-class (classoid-name type))))
79 ((specializerp type) type)))
82 (defun type-from-specializer (specl)
86 (unless (member (car specl) '(class prototype class-eq eql))
87 (error "~S is not a legal specializer type." specl))
91 ;;maybe (or (find-class specl nil) (ensure-class specl)) instead?
92 (setq specl (find-class specl)))
93 (or (not (eq *boot-state* 'complete))
94 (specializerp specl)))
95 (specializer-type specl))
97 (error "~S is neither a type nor a specializer." specl))))
99 (defun type-class (type)
100 (declare (special *the-class-t*))
101 (setq type (type-from-specializer type))
105 (error "bad argument to TYPE-CLASS"))
107 (eql (class-of (cadr type)))
108 (prototype (class-of (cadr type))) ;?
109 (class-eq (cadr type))
110 (class (cadr type)))))
112 (defun class-eq-type (class)
113 (specializer-type (class-eq-specializer class)))
115 ;;; internal to this file..
117 ;;; These functions are a pale imitation of their namesake. They accept
118 ;;; class objects or types where they should.
119 (defun *normalize-type (type)
121 (if (member (car type) '(not and or))
122 `(,(car type) ,@(mapcar #'*normalize-type (cdr type)))
123 (if (null (cdr type))
124 (*normalize-type (car type))
127 (let ((class (find-class type nil)))
129 (let ((type (specializer-type class)))
130 (if (listp type) type `(,type)))
132 ((or (not (eq *boot-state* 'complete))
134 (specializer-type type))
136 (error "~S is not a type." type))))
138 ;;; internal to this file...
139 (defun convert-to-system-type (type)
141 ((not and or) `(,(car type) ,@(mapcar #'convert-to-system-type
143 ((class class-eq) ; class-eq is impossible to do right
144 (layout-classoid (class-wrapper (cadr type))))
146 (t (if (null (cdr type))
150 ;;; Writing the missing NOT and AND clauses will improve the quality
151 ;;; of code generated by GENERATE-DISCRIMINATION-NET, but calling
152 ;;; SUBTYPEP in place of just returning (VALUES NIL NIL) can be very
153 ;;; slow. *SUBTYPEP is used by PCL itself, and must be fast.
155 ;;; FIXME: SB-KERNEL has fast-and-not-quite-precise type code for use
156 ;;; in the compiler. Could we share some of it here?
157 (defun *subtypep (type1 type2)
158 (if (equal type1 type2)
160 (if (eq *boot-state* 'early)
161 (values (eq type1 type2) t)
162 (let ((*in-precompute-effective-methods-p* t))
163 (declare (special *in-precompute-effective-methods-p*))
164 ;; FIXME: *IN-PRECOMPUTE-EFFECTIVE-METHODS-P* is not a
165 ;; good name. It changes the way
166 ;; CLASS-APPLICABLE-USING-CLASS-P works.
167 (setq type1 (*normalize-type type1))
168 (setq type2 (*normalize-type type2))
171 (values nil nil)) ; XXX We should improve this.
173 (values nil nil)) ; XXX We should improve this.
174 ((eql wrapper-eq class-eq class)
175 (multiple-value-bind (app-p maybe-app-p)
176 (specializer-applicable-using-type-p type2 type1)
177 (values app-p (or app-p (not maybe-app-p)))))
179 (subtypep (convert-to-system-type type1)
180 (convert-to-system-type type2))))))))
182 (defvar *built-in-class-symbols* ())
183 (defvar *built-in-wrapper-symbols* ())
185 (defun get-built-in-class-symbol (class-name)
186 (or (cadr (assq class-name *built-in-class-symbols*))
187 (let ((symbol (make-class-symbol class-name)))
188 (push (list class-name symbol) *built-in-class-symbols*)
191 (defun get-built-in-wrapper-symbol (class-name)
192 (or (cadr (assq class-name *built-in-wrapper-symbols*))
193 (let ((symbol (make-wrapper-symbol class-name)))
194 (push (list class-name symbol) *built-in-wrapper-symbols*)
197 (pushnew '%class *var-declarations*)
198 (pushnew '%variable-rebinding *var-declarations*)
200 (defun variable-class (var env)
201 (caddr (var-declaration 'class var env)))
203 (defvar *name->class->slotd-table* (make-hash-table))
205 (defvar *standard-method-combination*)
207 (defun plist-value (object name)
208 (getf (object-plist object) name))
210 (defun (setf plist-value) (new-value object name)
212 (setf (getf (object-plist object) name) new-value)
214 (remf (object-plist object) name)
217 ;;;; built-in classes
219 ;;; Grovel over SB-KERNEL::*BUILT-IN-CLASSES* in order to set
220 ;;; SB-PCL:*BUILT-IN-CLASSES*.
221 (/show "about to set up SB-PCL::*BUILT-IN-CLASSES*")
222 (defvar *built-in-classes*
223 (labels ((direct-supers (class)
224 (/noshow "entering DIRECT-SUPERS" (classoid-name class))
225 (if (typep class 'built-in-classoid)
226 (built-in-classoid-direct-superclasses class)
227 (let ((inherits (layout-inherits
228 (classoid-layout class))))
230 (list (svref inherits (1- (length inherits)))))))
232 (/noshow "entering DIRECT-SUBS" (classoid-name class))
234 (let ((subs (classoid-subclasses class)))
240 (when (member class (direct-supers sub))
243 (mapcar (lambda (kernel-bic-entry)
244 (/noshow "setting up" kernel-bic-entry)
245 (let* ((name (car kernel-bic-entry))
246 (class (find-classoid name))
248 (getf (cdr kernel-bic-entry) :prototype-form)))
251 ,(mapcar #'classoid-name (direct-supers class))
252 ,(mapcar #'classoid-name (direct-subs class))
256 (layout-classoid x)))
259 (classoid-layout class))))
261 (eval prototype-form)
262 ;; This is the default prototype value which
263 ;; was used, without explanation, by the CMU CL
264 ;; code we're derived from. Evidently it's safe
265 ;; in all relevant cases.
267 (remove-if (lambda (kernel-bic-entry)
268 (member (first kernel-bic-entry)
269 ;; I'm not sure why these are removed from
270 ;; the list, but that's what the original
271 ;; CMU CL code did. -- WHN 20000715
273 file-stream string-stream)))
274 sb-kernel::*built-in-classes*))))
275 (/noshow "done setting up SB-PCL::*BUILT-IN-CLASSES*")
277 ;;;; the classes that define the kernel of the metabraid
280 (:metaclass built-in-class))
282 (defclass function (t) ()
283 (:metaclass built-in-class))
285 (defclass stream (t) ()
286 (:metaclass built-in-class))
288 (defclass file-stream (stream) ()
289 (:metaclass built-in-class))
291 (defclass string-stream (stream) ()
292 (:metaclass built-in-class))
294 (defclass slot-object (t) ()
295 (:metaclass slot-class))
297 (defclass condition (slot-object) ()
298 (:metaclass condition-class))
300 (defclass structure-object (slot-object) ()
301 (:metaclass structure-class))
303 (defstruct (dead-beef-structure-object
304 (:constructor |STRUCTURE-OBJECT class constructor|)
307 (defclass standard-object (slot-object) ())
309 (defclass funcallable-standard-object (standard-object function)
311 (:metaclass funcallable-standard-class))
313 (defclass metaobject (standard-object) ())
315 (defclass generic-function (dependent-update-mixin
316 definition-source-mixin
318 funcallable-standard-object)
321 :initarg :documentation)
322 ;; We need to make a distinction between the methods initially set
323 ;; up by :METHOD options to DEFGENERIC and the ones set up later by
324 ;; DEFMETHOD, because ANSI specifies that executing DEFGENERIC on
325 ;; an already-DEFGENERICed function clears the methods set by the
326 ;; previous DEFGENERIC, but not methods set by DEFMETHOD. (Making
327 ;; this distinction seems a little kludgy, but it has the positive
328 ;; effect of making it so that loading a file a.lisp containing
329 ;; DEFGENERIC, then loading a second file b.lisp containing
330 ;; DEFMETHOD, then modifying and reloading a.lisp and/or b.lisp
331 ;; tends to leave the generic function in a state consistent with
332 ;; the most-recently-loaded state of a.lisp and b.lisp.)
335 :accessor generic-function-initial-methods))
336 (:metaclass funcallable-standard-class))
338 (defclass standard-generic-function (generic-function)
342 :reader generic-function-name)
345 :accessor generic-function-methods
348 :initarg :method-class
349 :accessor generic-function-method-class)
351 :initarg :method-combination
352 :accessor generic-function-method-combination)
354 ;; KLUDGE: AMOP specifies :DECLARATIONS, while ANSI specifies
355 ;; :DECLARE. Allow either (but FIXME: maybe a note or a warning
356 ;; might be appropriate).
357 :initarg :declarations
360 :accessor generic-function-declarations)
362 :initform (make-arg-info)
366 :accessor gf-dfun-state))
367 (:metaclass funcallable-standard-class)
368 (:default-initargs :method-class *the-class-standard-method*
369 :method-combination *standard-method-combination*))
371 (defclass method (metaobject) ())
373 (defclass standard-method (definition-source-mixin plist-mixin method)
376 :accessor method-generic-function)
379 ;;; :initarg :qualifiers
380 ;;; :reader method-qualifiers)
383 :initarg :specializers
384 :reader method-specializers)
387 :initarg :lambda-list
388 :reader method-lambda-list)
391 :initarg :function) ;no writer
394 :initarg :fast-function ;no writer
395 :reader method-fast-function)
398 :initarg :documentation)))
400 (defclass standard-accessor-method (standard-method)
401 ((slot-name :initform nil
403 :reader accessor-method-slot-name)
404 (slot-definition :initform nil
405 :initarg :slot-definition
406 :reader accessor-method-slot-definition)))
408 (defclass standard-reader-method (standard-accessor-method) ())
409 (defclass standard-writer-method (standard-accessor-method) ())
410 ;;; an extension, apparently.
411 (defclass standard-boundp-method (standard-accessor-method) ())
413 (defclass method-combination (metaobject)
415 :reader method-combination-documentation
417 :initarg :documentation)))
419 (defclass standard-method-combination (definition-source-mixin
422 :reader method-combination-type
425 :reader method-combination-options
428 (defclass long-method-combination (standard-method-combination)
431 :reader long-method-combination-function)
433 :initarg :args-lambda-list
434 :reader long-method-combination-args-lambda-list)))
436 (defclass short-method-combination (standard-method-combination)
438 :reader short-combination-operator
440 (identity-with-one-argument
441 :reader short-combination-identity-with-one-argument
442 :initarg :identity-with-one-argument)))
444 (defclass slot-definition (metaobject)
448 :accessor slot-definition-name)
452 :accessor slot-definition-initform)
455 :initarg :initfunction
456 :accessor slot-definition-initfunction)
460 :accessor slot-definition-readers)
464 :accessor slot-definition-writers)
468 :accessor slot-definition-initargs)
472 :accessor slot-definition-type)
475 :initarg :documentation
476 ;; FIXME: should we export this, as an extension?
477 :accessor %slot-definition-documentation)
481 :accessor slot-definition-class)))
483 (defclass standard-slot-definition (slot-definition)
487 :accessor slot-definition-allocation)
490 :initarg :allocation-class
491 :accessor slot-definition-allocation-class)))
493 (defclass condition-slot-definition (slot-definition)
497 :accessor slot-definition-allocation)
500 :initarg :allocation-class
501 :accessor slot-definition-allocation-class)))
503 (defclass structure-slot-definition (slot-definition)
504 ((defstruct-accessor-symbol
506 :initarg :defstruct-accessor-symbol
507 :accessor slot-definition-defstruct-accessor-symbol)
508 (internal-reader-function
510 :initarg :internal-reader-function
511 :accessor slot-definition-internal-reader-function)
512 (internal-writer-function
514 :initarg :internal-writer-function
515 :accessor slot-definition-internal-writer-function)))
517 (defclass direct-slot-definition (slot-definition)
520 (defclass effective-slot-definition (slot-definition)
521 ((reader-function ; (lambda (object) ...)
522 :accessor slot-definition-reader-function)
523 (writer-function ; (lambda (new-value object) ...)
524 :accessor slot-definition-writer-function)
525 (boundp-function ; (lambda (object) ...)
526 :accessor slot-definition-boundp-function)
530 (defclass standard-direct-slot-definition (standard-slot-definition
531 direct-slot-definition)
534 (defclass standard-effective-slot-definition (standard-slot-definition
535 effective-slot-definition)
536 ((location ; nil, a fixnum, a cons: (slot-name . value)
538 :accessor slot-definition-location)))
540 (defclass condition-direct-slot-definition (condition-slot-definition
541 direct-slot-definition)
544 (defclass condition-effective-slot-definition (condition-slot-definition
545 effective-slot-definition)
548 (defclass structure-direct-slot-definition (structure-slot-definition
549 direct-slot-definition)
552 (defclass structure-effective-slot-definition (structure-slot-definition
553 effective-slot-definition)
556 (defclass specializer (metaobject)
557 ((type :initform nil :reader specializer-type)))
559 (defclass specializer-with-object (specializer) ())
561 (defclass exact-class-specializer (specializer) ())
563 (defclass class-eq-specializer (exact-class-specializer
564 specializer-with-object)
565 ((object :initarg :class
566 :reader specializer-class
567 :reader specializer-object)))
569 (defclass class-prototype-specializer (specializer-with-object)
570 ((object :initarg :class
571 :reader specializer-class
572 :reader specializer-object)))
574 (defclass eql-specializer (exact-class-specializer specializer-with-object)
575 ((object :initarg :object :reader specializer-object
576 :reader eql-specializer-object)))
578 (defvar *eql-specializer-table* (make-hash-table :test 'eql))
580 (defun intern-eql-specializer (object)
581 (or (gethash object *eql-specializer-table*)
582 (setf (gethash object *eql-specializer-table*)
583 (make-instance 'eql-specializer :object object))))
585 (defclass class (dependent-update-mixin
586 definition-source-mixin
592 (class-eq-specializer
594 :reader class-eq-specializer)
597 :reader class-direct-superclasses)
598 ;; Note: The (CLASS-)DIRECT-SUBCLASSES for STRUCTURE-CLASSes and
599 ;; CONDITION-CLASSes are lazily computed whenever the subclass info
600 ;; becomes available, i.e. when the PCL class is created.
603 :reader class-direct-subclasses)
605 :initform (cons nil nil))
608 :initarg :documentation)
611 :reader class-finalized-p)))
613 (def!method make-load-form ((class class) &optional env)
614 ;; FIXME: should we not instead pass ENV to FIND-CLASS? Probably
615 ;; doesn't matter while all our environments are the same...
616 (declare (ignore env))
617 (let ((name (class-name class)))
618 (unless (and name (eq (find-class name nil) class))
619 (error "~@<Can't use anonymous or undefined class as constant: ~S~:@>"
621 `(find-class ',name)))
623 ;;; The class PCL-CLASS is an implementation-specific common
624 ;;; superclass of all specified subclasses of the class CLASS.
625 (defclass pcl-class (class)
626 ((class-precedence-list
627 :reader class-precedence-list)
628 ;; KLUDGE: see note in CPL-OR-NIL
630 :reader cpl-available-p
634 :reader class-can-precede-list)
635 (incompatible-superclass-list
637 :accessor class-incompatible-superclass-list)
640 :reader class-wrapper)
643 :reader class-prototype)))
645 (defclass slot-class (pcl-class)
648 :accessor class-direct-slots)
651 :accessor class-slots)))
653 ;;; The class STD-CLASS is an implementation-specific common
654 ;;; superclass of the classes STANDARD-CLASS and
655 ;;; FUNCALLABLE-STANDARD-CLASS.
656 (defclass std-class (slot-class)
659 (defclass standard-class (std-class)
662 (defclass funcallable-standard-class (std-class)
665 (defclass forward-referenced-class (pcl-class) ())
667 (defclass built-in-class (pcl-class) ())
669 (defclass condition-class (slot-class) ())
671 (defclass structure-class (slot-class)
674 :accessor class-defstruct-form)
675 (defstruct-constructor
677 :accessor class-defstruct-constructor)
680 :initarg :from-defclass-p)))
682 (defclass definition-source-mixin (standard-object)
685 :reader definition-source
686 :initarg :definition-source)))
688 (defclass plist-mixin (standard-object)
689 ((plist :initform () :accessor object-plist)))
691 (defclass dependent-update-mixin (plist-mixin) ())
693 (defparameter *early-class-predicates*
694 '((specializer specializerp)
695 (exact-class-specializer exact-class-specializer-p)
696 (class-eq-specializer class-eq-specializer-p)
697 (eql-specializer eql-specializer-p)
699 (slot-class slot-class-p)
700 (std-class std-class-p)
701 (standard-class standard-class-p)
702 (funcallable-standard-class funcallable-standard-class-p)
703 (condition-class condition-class-p)
704 (structure-class structure-class-p)
705 (forward-referenced-class forward-referenced-class-p)
707 (standard-method standard-method-p)
708 (standard-accessor-method standard-accessor-method-p)
709 (standard-reader-method standard-reader-method-p)
710 (standard-writer-method standard-writer-method-p)
711 (standard-boundp-method standard-boundp-method-p)
712 (generic-function generic-function-p)
713 (standard-generic-function standard-generic-function-p)
714 (method-combination method-combination-p)
715 (long-method-combination long-method-combination-p)
716 (short-method-combination short-method-combination-p)))