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 ;;;; type specifier hackery
88 ;;; internal to this file
89 (defun coerce-to-class (class &optional make-forward-referenced-class-p)
91 (or (find-class class (not make-forward-referenced-class-p))
96 (defun specializer-from-type (type &aux args)
98 (setq args (cdr type) type (car type)))
100 (or (and (null args) (find-class type))
102 (class (coerce-to-class (car args)))
103 (prototype (make-instance 'class-prototype-specializer
104 :object (coerce-to-class (car args))))
105 (class-eq (class-eq-specializer (coerce-to-class (car args))))
106 (eql (intern-eql-specializer (car args))))))
107 ;; FIXME: do we still need this?
108 ((and (null args) (typep type 'classoid))
109 (or (classoid-pcl-class type)
110 (ensure-non-standard-class (classoid-name type))))
111 ((specializerp type) type)))
114 (defun type-from-specializer (specl)
118 (unless (member (car specl) '(class prototype class-eq eql))
119 (error "~S is not a legal specializer type." specl))
122 (when (symbolp specl)
123 ;;maybe (or (find-class specl nil) (ensure-class specl)) instead?
124 (setq specl (find-class specl)))
125 (or (not (eq *boot-state* 'complete))
126 (specializerp specl)))
127 (specializer-type specl))
129 (error "~S is neither a type nor a specializer." specl))))
131 (defun type-class (type)
132 (declare (special *the-class-t*))
133 (setq type (type-from-specializer type))
137 (error "bad argument to TYPE-CLASS"))
139 (eql (class-of (cadr type)))
140 (prototype (class-of (cadr type))) ;?
141 (class-eq (cadr type))
142 (class (cadr type)))))
144 (defun class-eq-type (class)
145 (specializer-type (class-eq-specializer class)))
147 ;;; internal to this file..
149 ;;; These functions are a pale imitation of their namesake. They accept
150 ;;; class objects or types where they should.
151 (defun *normalize-type (type)
153 (if (member (car type) '(not and or))
154 `(,(car type) ,@(mapcar #'*normalize-type (cdr type)))
155 (if (null (cdr type))
156 (*normalize-type (car type))
159 (let ((class (find-class type nil)))
161 (let ((type (specializer-type class)))
162 (if (listp type) type `(,type)))
164 ((or (not (eq *boot-state* 'complete))
166 (specializer-type type))
168 (error "~S is not a type." type))))
170 ;;; internal to this file...
171 (defun convert-to-system-type (type)
173 ((not and or) `(,(car type) ,@(mapcar #'convert-to-system-type
175 ((class class-eq) ; class-eq is impossible to do right
176 (layout-classoid (class-wrapper (cadr type))))
178 (t (if (null (cdr type))
182 ;;; Writing the missing NOT and AND clauses will improve the quality
183 ;;; of code generated by GENERATE-DISCRIMINATION-NET, but calling
184 ;;; SUBTYPEP in place of just returning (VALUES NIL NIL) can be very
185 ;;; slow. *SUBTYPEP is used by PCL itself, and must be fast.
187 ;;; FIXME: SB-KERNEL has fast-and-not-quite-precise type code for use
188 ;;; in the compiler. Could we share some of it here?
189 (defun *subtypep (type1 type2)
190 (if (equal type1 type2)
192 (if (eq *boot-state* 'early)
193 (values (eq type1 type2) t)
194 (let ((*in-precompute-effective-methods-p* t))
195 (declare (special *in-precompute-effective-methods-p*))
196 ;; FIXME: *IN-PRECOMPUTE-EFFECTIVE-METHODS-P* is not a
197 ;; good name. It changes the way
198 ;; CLASS-APPLICABLE-USING-CLASS-P works.
199 (setq type1 (*normalize-type type1))
200 (setq type2 (*normalize-type type2))
203 (values nil nil)) ; XXX We should improve this.
205 (values nil nil)) ; XXX We should improve this.
206 ((eql wrapper-eq class-eq class)
207 (multiple-value-bind (app-p maybe-app-p)
208 (specializer-applicable-using-type-p type2 type1)
209 (values app-p (or app-p (not maybe-app-p)))))
211 (subtypep (convert-to-system-type type1)
212 (convert-to-system-type type2))))))))
214 (defvar *built-in-class-symbols* ())
215 (defvar *built-in-wrapper-symbols* ())
217 (defun get-built-in-class-symbol (class-name)
218 (or (cadr (assq class-name *built-in-class-symbols*))
219 (let ((symbol (make-class-symbol class-name)))
220 (push (list class-name symbol) *built-in-class-symbols*)
223 (defun get-built-in-wrapper-symbol (class-name)
224 (or (cadr (assq class-name *built-in-wrapper-symbols*))
225 (let ((symbol (make-wrapper-symbol class-name)))
226 (push (list class-name symbol) *built-in-wrapper-symbols*)
229 (pushnew '%class *var-declarations*)
230 (pushnew '%variable-rebinding *var-declarations*)
232 (defun variable-class (var env)
233 (caddr (var-declaration 'class var env)))
235 (defvar *name->class->slotd-table* (make-hash-table))
237 (defvar *standard-method-combination*)
239 (defun make-class-predicate-name (name)
240 (list 'class-predicate name))
242 (defun plist-value (object name)
243 (getf (object-plist object) name))
245 (defun (setf plist-value) (new-value object name)
247 (setf (getf (object-plist object) name) new-value)
249 (remf (object-plist object) name)
252 ;;;; built-in classes
254 ;;; Grovel over SB-KERNEL::*BUILT-IN-CLASSES* in order to set
255 ;;; SB-PCL:*BUILT-IN-CLASSES*.
256 (/show "about to set up SB-PCL::*BUILT-IN-CLASSES*")
257 (defvar *built-in-classes*
258 (labels ((direct-supers (class)
259 (/noshow "entering DIRECT-SUPERS" (classoid-name class))
260 (if (typep class 'built-in-classoid)
261 (built-in-classoid-direct-superclasses class)
262 (let ((inherits (layout-inherits
263 (classoid-layout class))))
265 (list (svref inherits (1- (length inherits)))))))
267 (/noshow "entering DIRECT-SUBS" (classoid-name class))
269 (let ((subs (classoid-subclasses class)))
275 (when (member class (direct-supers sub))
278 (mapcar (lambda (kernel-bic-entry)
279 (/noshow "setting up" kernel-bic-entry)
280 (let* ((name (car kernel-bic-entry))
281 (class (find-classoid name))
283 (getf (cdr kernel-bic-entry) :prototype-form)))
286 ,(mapcar #'classoid-name (direct-supers class))
287 ,(mapcar #'classoid-name (direct-subs class))
291 (layout-classoid x)))
294 (classoid-layout class))))
296 (eval prototype-form)
297 ;; This is the default prototype value which
298 ;; was used, without explanation, by the CMU CL
299 ;; code we're derived from. Evidently it's safe
300 ;; in all relevant cases.
302 (remove-if (lambda (kernel-bic-entry)
303 (member (first kernel-bic-entry)
304 ;; I'm not sure why these are removed from
305 ;; the list, but that's what the original
306 ;; CMU CL code did. -- WHN 20000715
308 file-stream string-stream)))
309 sb-kernel::*built-in-classes*))))
310 (/noshow "done setting up SB-PCL::*BUILT-IN-CLASSES*")
312 ;;;; the classes that define the kernel of the metabraid
315 (:metaclass built-in-class))
317 (defclass function (t) ()
318 (:metaclass built-in-class))
320 (defclass stream (t) ()
321 (:metaclass built-in-class))
323 (defclass file-stream (stream) ()
324 (:metaclass built-in-class))
326 (defclass string-stream (stream) ()
327 (:metaclass built-in-class))
329 (defclass slot-object (t) ()
330 (:metaclass slot-class))
332 (defclass condition (slot-object) ()
333 (:metaclass condition-class))
335 (defclass structure-object (slot-object) ()
336 (:metaclass structure-class))
338 (defstruct (dead-beef-structure-object
339 (:constructor |STRUCTURE-OBJECT class constructor|)
342 (defclass standard-object (slot-object) ())
344 (defclass funcallable-standard-object (standard-object function)
346 (:metaclass funcallable-standard-class))
348 (defclass metaobject (standard-object) ())
350 (defclass generic-function (dependent-update-mixin
351 definition-source-mixin
353 funcallable-standard-object)
356 :initarg :documentation)
357 ;; We need to make a distinction between the methods initially set
358 ;; up by :METHOD options to DEFGENERIC and the ones set up later by
359 ;; DEFMETHOD, because ANSI specifies that executing DEFGENERIC on
360 ;; an already-DEFGENERICed function clears the methods set by the
361 ;; previous DEFGENERIC, but not methods set by DEFMETHOD. (Making
362 ;; this distinction seems a little kludgy, but it has the positive
363 ;; effect of making it so that loading a file a.lisp containing
364 ;; DEFGENERIC, then loading a second file b.lisp containing
365 ;; DEFMETHOD, then modifying and reloading a.lisp and/or b.lisp
366 ;; tends to leave the generic function in a state consistent with
367 ;; the most-recently-loaded state of a.lisp and b.lisp.)
370 :accessor generic-function-initial-methods))
371 (:metaclass funcallable-standard-class))
373 (defclass standard-generic-function (generic-function)
377 :accessor generic-function-name)
380 :accessor generic-function-methods
383 :initarg :method-class
384 :accessor generic-function-method-class)
386 :initarg :method-combination
387 :accessor generic-function-method-combination)
389 ;; KLUDGE: AMOP specifies :DECLARATIONS, while ANSI specifies
390 ;; :DECLARE. Allow either (but FIXME: maybe a note or a warning
391 ;; might be appropriate).
392 :initarg :declarations
395 :accessor generic-function-declarations)
397 :initform (make-arg-info)
401 :accessor gf-dfun-state))
402 (:metaclass funcallable-standard-class)
403 (:default-initargs :method-class *the-class-standard-method*
404 :method-combination *standard-method-combination*))
406 (defclass method (metaobject) ())
408 (defclass standard-method (definition-source-mixin plist-mixin method)
411 :accessor method-generic-function)
414 ;;; :initarg :qualifiers
415 ;;; :reader method-qualifiers)
418 :initarg :specializers
419 :reader method-specializers)
422 :initarg :lambda-list
423 :reader method-lambda-list)
426 :initarg :function) ;no writer
429 :initarg :fast-function ;no writer
430 :reader method-fast-function)
433 :initarg :documentation)))
435 (defclass standard-accessor-method (standard-method)
436 ((slot-name :initform nil
438 :reader accessor-method-slot-name)
439 (slot-definition :initform nil
440 :initarg :slot-definition
441 :reader accessor-method-slot-definition)))
443 (defclass standard-reader-method (standard-accessor-method) ())
444 (defclass standard-writer-method (standard-accessor-method) ())
445 ;;; an extension, apparently.
446 (defclass standard-boundp-method (standard-accessor-method) ())
448 (defclass method-combination (metaobject)
450 :reader method-combination-documentation
452 :initarg :documentation)))
454 (defclass standard-method-combination (definition-source-mixin
457 :reader method-combination-type
460 :reader method-combination-options
463 (defclass long-method-combination (standard-method-combination)
466 :reader long-method-combination-function)
468 :initarg :args-lambda-list
469 :reader long-method-combination-args-lambda-list)))
471 (defclass slot-definition (metaobject)
475 :accessor slot-definition-name)
479 :accessor slot-definition-initform)
482 :initarg :initfunction
483 :accessor slot-definition-initfunction)
487 :accessor slot-definition-readers)
491 :accessor slot-definition-writers)
495 :accessor slot-definition-initargs)
499 :accessor slot-definition-type)
502 :initarg :documentation)
506 :accessor slot-definition-class)))
508 (defclass standard-slot-definition (slot-definition)
512 :accessor slot-definition-allocation)
515 :initarg :allocation-class
516 :accessor slot-definition-allocation-class)))
518 (defclass condition-slot-definition (slot-definition)
522 :accessor slot-definition-allocation)
525 :initarg :allocation-class
526 :accessor slot-definition-allocation-class)))
528 (defclass structure-slot-definition (slot-definition)
529 ((defstruct-accessor-symbol
531 :initarg :defstruct-accessor-symbol
532 :accessor slot-definition-defstruct-accessor-symbol)
533 (internal-reader-function
535 :initarg :internal-reader-function
536 :accessor slot-definition-internal-reader-function)
537 (internal-writer-function
539 :initarg :internal-writer-function
540 :accessor slot-definition-internal-writer-function)))
542 (defclass direct-slot-definition (slot-definition)
545 (defclass effective-slot-definition (slot-definition)
546 ((reader-function ; (lambda (object) ...)
547 :accessor slot-definition-reader-function)
548 (writer-function ; (lambda (new-value object) ...)
549 :accessor slot-definition-writer-function)
550 (boundp-function ; (lambda (object) ...)
551 :accessor slot-definition-boundp-function)
555 (defclass standard-direct-slot-definition (standard-slot-definition
556 direct-slot-definition)
559 (defclass standard-effective-slot-definition (standard-slot-definition
560 effective-slot-definition)
561 ((location ; nil, a fixnum, a cons: (slot-name . value)
563 :accessor slot-definition-location)))
565 (defclass condition-direct-slot-definition (condition-slot-definition
566 direct-slot-definition)
569 (defclass condition-effective-slot-definition (condition-slot-definition
570 effective-slot-definition)
573 (defclass structure-direct-slot-definition (structure-slot-definition
574 direct-slot-definition)
577 (defclass structure-effective-slot-definition (structure-slot-definition
578 effective-slot-definition)
581 (defclass specializer (metaobject)
582 ((type :initform nil :reader specializer-type)))
584 (defclass specializer-with-object (specializer) ())
586 (defclass exact-class-specializer (specializer) ())
588 (defclass class-eq-specializer (exact-class-specializer
589 specializer-with-object)
590 ((object :initarg :class
591 :reader specializer-class
592 :reader specializer-object)))
594 (defclass class-prototype-specializer (specializer-with-object)
595 ((object :initarg :class
596 :reader specializer-class
597 :reader specializer-object)))
599 (defclass eql-specializer (exact-class-specializer specializer-with-object)
600 ((object :initarg :object :reader specializer-object
601 :reader eql-specializer-object)))
603 (defvar *eql-specializer-table* (make-hash-table :test 'eql))
605 (defun intern-eql-specializer (object)
606 (or (gethash object *eql-specializer-table*)
607 (setf (gethash object *eql-specializer-table*)
608 (make-instance 'eql-specializer :object object))))
610 (defclass class (dependent-update-mixin
611 definition-source-mixin
616 :accessor class-name)
617 (class-eq-specializer
619 :reader class-eq-specializer)
622 :reader class-direct-superclasses)
623 ;; Note: The (CLASS-)DIRECT-SUBCLASSES for STRUCTURE-CLASSes and
624 ;; CONDITION-CLASSes are lazily computed whenever the subclass info
625 ;; becomes available, i.e. when the PCL class is created.
628 :reader class-direct-subclasses)
630 :initform (cons nil nil))
633 :reader class-predicate-name)
636 :initarg :documentation)
639 :reader class-finalized-p)))
641 (def!method make-load-form ((class class) &optional env)
642 ;; FIXME: should we not instead pass ENV to FIND-CLASS? Probably
643 ;; doesn't matter while all our environments are the same...
644 (declare (ignore env))
645 (let ((name (class-name class)))
646 (unless (and name (eq (find-class name nil) class))
647 (error "~@<Can't use anonymous or undefined class as constant: ~S~:@>"
649 `(find-class ',name)))
651 ;;; The class PCL-CLASS is an implementation-specific common
652 ;;; superclass of all specified subclasses of the class CLASS.
653 (defclass pcl-class (class)
654 ((class-precedence-list
655 :reader class-precedence-list)
656 ;; KLUDGE: see note in CPL-OR-NIL
658 :reader cpl-available-p
662 :reader class-can-precede-list)
663 (incompatible-superclass-list
665 :accessor class-incompatible-superclass-list)
668 :reader class-wrapper)
671 :reader class-prototype)))
673 (defclass slot-class (pcl-class)
676 :accessor class-direct-slots)
679 :accessor class-slots)))
681 ;;; The class STD-CLASS is an implementation-specific common
682 ;;; superclass of the classes STANDARD-CLASS and
683 ;;; FUNCALLABLE-STANDARD-CLASS.
684 (defclass std-class (slot-class)
687 (defclass standard-class (std-class)
690 (defclass funcallable-standard-class (std-class)
693 (defclass forward-referenced-class (pcl-class) ())
695 (defclass built-in-class (pcl-class) ())
697 (defclass condition-class (slot-class) ())
699 (defclass structure-class (slot-class)
702 :accessor class-defstruct-form)
703 (defstruct-constructor
705 :accessor class-defstruct-constructor)
708 :initarg :from-defclass-p)))
710 (defclass definition-source-mixin (standard-object)
711 ((source :initform *load-pathname* :reader definition-source
712 :initarg :definition-source)))
714 (defclass plist-mixin (standard-object)
715 ((plist :initform () :accessor object-plist)))
717 (defclass dependent-update-mixin (plist-mixin) ())
719 (defparameter *early-class-predicates*
720 '((specializer specializerp)
721 (exact-class-specializer exact-class-specializer-p)
722 (class-eq-specializer class-eq-specializer-p)
723 (eql-specializer eql-specializer-p)
725 (slot-class slot-class-p)
726 (std-class std-class-p)
727 (standard-class standard-class-p)
728 (funcallable-standard-class funcallable-standard-class-p)
729 (condition-class condition-class-p)
730 (structure-class structure-class-p)
731 (forward-referenced-class forward-referenced-class-p)
733 (standard-method standard-method-p)
734 (standard-accessor-method standard-accessor-method-p)
735 (standard-reader-method standard-reader-method-p)
736 (standard-writer-method standard-writer-method-p)
737 (standard-boundp-method standard-boundp-method-p)
738 (generic-function generic-function-p)
739 (standard-generic-function standard-generic-function-p)
740 (method-combination method-combination-p)
741 (long-method-combination long-method-combination-p)))