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 (return-from specializer-from-type
(find-class type
)))
68 (setq args
(cdr type
) type
(car type
)))
71 (class (coerce-to-class (car args
)))
72 (prototype (make-instance 'class-prototype-specializer
73 :object
(coerce-to-class (car args
))))
74 (class-eq (class-eq-specializer (coerce-to-class (car args
))))
75 (eql (intern-eql-specializer (car args
))))))
76 ;; FIXME: do we still need this?
77 ((and (null args
) (typep type
'classoid
))
78 (or (classoid-pcl-class type
)
79 (ensure-non-standard-class (classoid-name type
))))
80 ((specializerp type
) type
)))
83 (defun type-from-specializer (specl)
87 (unless (member (car specl
) '(class prototype class-eq eql
))
88 (error "~S is not a legal specializer type." specl
))
92 ;;maybe (or (find-class specl nil) (ensure-class specl)) instead?
93 (setq specl
(find-class specl
)))
94 (or (not (eq *boot-state
* 'complete
))
95 (specializerp specl
)))
96 (specializer-type specl
))
98 (error "~S is neither a type nor a specializer." specl
))))
100 (defun type-class (type)
101 (declare (special *the-class-t
*))
102 (setq type
(type-from-specializer type
))
106 (error "bad argument to TYPE-CLASS"))
108 (eql (class-of (cadr type
)))
109 (prototype (class-of (cadr type
))) ;?
110 (class-eq (cadr type
))
111 (class (cadr type
)))))
113 (defun class-eq-type (class)
114 (specializer-type (class-eq-specializer class
)))
116 ;;; internal to this file..
118 ;;; These functions are a pale imitation of their namesake. They accept
119 ;;; class objects or types where they should.
120 (defun *normalize-type
(type)
122 (if (member (car type
) '(not and or
))
123 `(,(car type
) ,@(mapcar #'*normalize-type
(cdr type
)))
124 (if (null (cdr type
))
125 (*normalize-type
(car type
))
128 (let ((class (find-class type nil
)))
130 (let ((type (specializer-type class
)))
131 (if (listp type
) type
`(,type
)))
133 ((or (not (eq *boot-state
* 'complete
))
135 (specializer-type type
))
137 (error "~S is not a type." type
))))
139 ;;; internal to this file...
140 (defun convert-to-system-type (type)
142 ((not and or
) `(,(car type
) ,@(mapcar #'convert-to-system-type
144 ((class class-eq
) ; class-eq is impossible to do right
145 (layout-classoid (class-wrapper (cadr type
))))
147 (t (if (null (cdr type
))
151 ;;; Writing the missing NOT and AND clauses will improve the quality
152 ;;; of code generated by GENERATE-DISCRIMINATION-NET, but calling
153 ;;; SUBTYPEP in place of just returning (VALUES NIL NIL) can be very
154 ;;; slow. *SUBTYPEP is used by PCL itself, and must be fast.
156 ;;; FIXME: SB-KERNEL has fast-and-not-quite-precise type code for use
157 ;;; in the compiler. Could we share some of it here?
158 (defun *subtypep
(type1 type2
)
159 (if (equal type1 type2
)
161 (if (eq *boot-state
* 'early
)
162 (values (eq type1 type2
) t
)
163 (let ((*in-precompute-effective-methods-p
* t
))
164 (declare (special *in-precompute-effective-methods-p
*))
165 ;; FIXME: *IN-PRECOMPUTE-EFFECTIVE-METHODS-P* is not a
166 ;; good name. It changes the way
167 ;; CLASS-APPLICABLE-USING-CLASS-P works.
168 (setq type1
(*normalize-type type1
))
169 (setq type2
(*normalize-type type2
))
172 (values nil nil
)) ; XXX We should improve this.
174 (values nil nil
)) ; XXX We should improve this.
175 ((eql wrapper-eq class-eq class
)
176 (multiple-value-bind (app-p maybe-app-p
)
177 (specializer-applicable-using-type-p type2 type1
)
178 (values app-p
(or app-p
(not maybe-app-p
)))))
180 (subtypep (convert-to-system-type type1
)
181 (convert-to-system-type type2
))))))))
183 (defvar *built-in-class-symbols
* ())
184 (defvar *built-in-wrapper-symbols
* ())
186 (defun get-built-in-class-symbol (class-name)
187 (or (cadr (assq class-name
*built-in-class-symbols
*))
188 (let ((symbol (make-class-symbol class-name
)))
189 (push (list class-name symbol
) *built-in-class-symbols
*)
192 (defun get-built-in-wrapper-symbol (class-name)
193 (or (cadr (assq class-name
*built-in-wrapper-symbols
*))
194 (let ((symbol (make-wrapper-symbol class-name
)))
195 (push (list class-name symbol
) *built-in-wrapper-symbols
*)
198 (defvar *standard-method-combination
*)
200 (defun plist-value (object name
)
201 (getf (object-plist object
) name
))
203 (defun (setf plist-value
) (new-value object name
)
205 (setf (getf (object-plist object
) name
) new-value
)
207 (remf (object-plist object
) name
)
210 ;;;; built-in classes
212 ;;; Grovel over SB-KERNEL::*BUILT-IN-CLASSES* in order to set
213 ;;; SB-PCL:*BUILT-IN-CLASSES*.
214 (/show
"about to set up SB-PCL::*BUILT-IN-CLASSES*")
215 (defvar *built-in-classes
*
216 (labels ((direct-supers (class)
217 (/noshow
"entering DIRECT-SUPERS" (classoid-name class
))
218 (if (typep class
'built-in-classoid
)
219 (built-in-classoid-direct-superclasses class
)
220 (let ((inherits (layout-inherits
221 (classoid-layout class
))))
223 (list (svref inherits
(1- (length inherits
)))))))
225 (/noshow
"entering DIRECT-SUBS" (classoid-name class
))
227 (let ((subs (classoid-subclasses class
)))
230 (dohash ((sub v
) subs
)
233 (when (member class
(direct-supers sub
))
236 (mapcar (lambda (kernel-bic-entry)
237 (/noshow
"setting up" kernel-bic-entry
)
238 (let* ((name (car kernel-bic-entry
))
239 (class (find-classoid name
))
241 (getf (cdr kernel-bic-entry
) :prototype-form
)))
244 ,(mapcar #'classoid-name
(direct-supers class
))
245 ,(mapcar #'classoid-name
(direct-subs class
))
249 (layout-classoid x
)))
252 (classoid-layout class
))))
254 (eval prototype-form
)
255 ;; This is the default prototype value which
256 ;; was used, without explanation, by the CMU CL
257 ;; code we're derived from. Evidently it's safe
258 ;; in all relevant cases.
260 (remove-if (lambda (kernel-bic-entry)
261 (member (first kernel-bic-entry
)
262 ;; I'm not sure why these are removed from
263 ;; the list, but that's what the original
264 ;; CMU CL code did. -- WHN 20000715
266 file-stream string-stream
)))
267 sb-kernel
::*built-in-classes
*))))
268 (/noshow
"done setting up SB-PCL::*BUILT-IN-CLASSES*")
270 ;;;; the classes that define the kernel of the metabraid
273 (:metaclass built-in-class
))
275 (defclass function
(t) ()
276 (:metaclass built-in-class
))
278 (defclass stream
(t) ()
279 (:metaclass built-in-class
))
281 (defclass file-stream
(stream) ()
282 (:metaclass built-in-class
))
284 (defclass string-stream
(stream) ()
285 (:metaclass built-in-class
))
287 (defclass slot-object
(t) ()
288 (:metaclass slot-class
))
290 (defclass condition
(slot-object) ()
291 (:metaclass condition-class
))
293 (defclass structure-object
(slot-object) ()
294 (:metaclass structure-class
))
296 (defstruct (dead-beef-structure-object
297 (:constructor |STRUCTURE-OBJECT class constructor|
)
300 (defclass standard-object
(slot-object) ())
302 (defclass funcallable-standard-object
(function standard-object
)
304 (:metaclass funcallable-standard-class
))
306 (defclass metaobject
(standard-object) ())
308 (defclass generic-function
(dependent-update-mixin
309 definition-source-mixin
311 funcallable-standard-object
)
314 :initarg
:documentation
)
315 ;; We need to make a distinction between the methods initially set
316 ;; up by :METHOD options to DEFGENERIC and the ones set up later by
317 ;; DEFMETHOD, because ANSI specifies that executing DEFGENERIC on
318 ;; an already-DEFGENERICed function clears the methods set by the
319 ;; previous DEFGENERIC, but not methods set by DEFMETHOD. (Making
320 ;; this distinction seems a little kludgy, but it has the positive
321 ;; effect of making it so that loading a file a.lisp containing
322 ;; DEFGENERIC, then loading a second file b.lisp containing
323 ;; DEFMETHOD, then modifying and reloading a.lisp and/or b.lisp
324 ;; tends to leave the generic function in a state consistent with
325 ;; the most-recently-loaded state of a.lisp and b.lisp.)
328 :accessor generic-function-initial-methods
))
329 (:metaclass funcallable-standard-class
))
331 (defclass standard-generic-function
(generic-function)
335 :reader generic-function-name
)
338 :accessor generic-function-methods
341 :initarg
:method-class
342 :accessor generic-function-method-class
)
344 :initarg
:method-combination
345 :accessor generic-function-method-combination
)
347 ;; KLUDGE: AMOP specifies :DECLARATIONS, while ANSI specifies
348 ;; :DECLARE. Allow either (but FIXME: maybe a note or a warning
349 ;; might be appropriate).
350 :initarg
:declarations
353 :accessor generic-function-declarations
)
355 :initform
(make-arg-info)
359 :accessor gf-dfun-state
)
360 ;; Used to make DFUN-STATE & FIN-FUNCTION updates atomic.
362 :initform
(sb-thread::make-spinlock
:name
"GF lock")
364 (:metaclass funcallable-standard-class
)
365 (:default-initargs
:method-class
*the-class-standard-method
*
366 :method-combination
*standard-method-combination
*))
368 (defclass method
(metaobject) ())
370 (defclass standard-method
(plist-mixin definition-source-mixin method
)
371 ((%generic-function
:initform nil
:accessor method-generic-function
)
372 (qualifiers :initform
() :initarg
:qualifiers
:reader method-qualifiers
)
373 (specializers :initform
() :initarg
:specializers
374 :reader method-specializers
)
375 (lambda-list :initform
() :initarg
:lambda-list
:reader method-lambda-list
)
376 (%function
:initform nil
:initarg
:function
:reader method-function
)
377 (%documentation
:initform nil
:initarg
:documentation
)))
379 (defclass accessor-method
(standard-method)
380 ((slot-name :initform nil
:initarg
:slot-name
381 :reader accessor-method-slot-name
)))
383 (defclass standard-accessor-method
(accessor-method)
384 ((%slot-definition
:initform nil
:initarg
:slot-definition
385 :reader accessor-method-slot-definition
)))
387 (defclass standard-reader-method
(standard-accessor-method) ())
388 (defclass standard-writer-method
(standard-accessor-method) ())
389 ;;; an extension, apparently.
390 (defclass standard-boundp-method
(standard-accessor-method) ())
392 ;;; for (SLOT-VALUE X 'FOO) / ACCESSOR-SLOT-VALUE optimization, which
393 ;;; can't be STANDARD-READER-METHOD because there is no associated
395 (defclass global-reader-method
(accessor-method) ())
396 (defclass global-writer-method
(accessor-method) ())
397 (defclass global-boundp-method
(accessor-method) ())
399 (defclass method-combination
(metaobject)
400 ((%documentation
:initform nil
:initarg
:documentation
)))
402 (defclass standard-method-combination
(definition-source-mixin
405 :reader method-combination-type-name
408 :reader method-combination-options
411 (defclass long-method-combination
(standard-method-combination)
414 :reader long-method-combination-function
)
416 :initarg
:args-lambda-list
417 :reader long-method-combination-args-lambda-list
)))
419 (defclass short-method-combination
(standard-method-combination)
421 :reader short-combination-operator
423 (identity-with-one-argument
424 :reader short-combination-identity-with-one-argument
425 :initarg
:identity-with-one-argument
)))
427 (defclass slot-definition
(metaobject)
431 :accessor slot-definition-name
)
435 :accessor slot-definition-initform
)
438 :initarg
:initfunction
439 :accessor slot-definition-initfunction
)
443 :accessor slot-definition-readers
)
447 :accessor slot-definition-writers
)
451 :accessor slot-definition-initargs
)
452 (%type
:initform t
:initarg
:type
:accessor slot-definition-type
)
453 (%type-check-function
:initform nil
454 :initarg type-check-function
455 :accessor slot-definition-type-check-function
)
457 :initform nil
:initarg
:documentation
458 ;; KLUDGE: we need a reader for bootstrapping purposes, in
459 ;; COMPUTE-EFFECTIVE-SLOT-DEFINITION-INITARGS.
460 :reader %slot-definition-documentation
)
461 (%class
:initform nil
:initarg
:class
:accessor slot-definition-class
)))
463 (defclass standard-slot-definition
(slot-definition)
467 :accessor slot-definition-allocation
)
470 :initarg
:allocation-class
471 :accessor slot-definition-allocation-class
)))
473 (defclass condition-slot-definition
(slot-definition)
477 :accessor slot-definition-allocation
)
480 :initarg
:allocation-class
481 :accessor slot-definition-allocation-class
)))
483 (defclass structure-slot-definition
(slot-definition)
484 ((defstruct-accessor-symbol
486 :initarg
:defstruct-accessor-symbol
487 :accessor slot-definition-defstruct-accessor-symbol
)
488 (internal-reader-function
490 :initarg
:internal-reader-function
491 :accessor slot-definition-internal-reader-function
)
492 (internal-writer-function
494 :initarg
:internal-writer-function
495 :accessor slot-definition-internal-writer-function
)))
497 (defclass direct-slot-definition
(slot-definition)
500 (defclass effective-slot-definition
(slot-definition)
501 ((reader-function ; (lambda (object) ...)
502 :accessor slot-definition-reader-function
)
503 (writer-function ; (lambda (new-value object) ...)
504 :accessor slot-definition-writer-function
)
505 (boundp-function ; (lambda (object) ...)
506 :accessor slot-definition-boundp-function
)
510 (defclass standard-direct-slot-definition
(standard-slot-definition
511 direct-slot-definition
)
514 (defclass standard-effective-slot-definition
(standard-slot-definition
515 effective-slot-definition
)
516 ((location ; nil, a fixnum, a cons: (slot-name . value)
518 :accessor slot-definition-location
)))
520 (defclass condition-direct-slot-definition
(condition-slot-definition
521 direct-slot-definition
)
524 (defclass condition-effective-slot-definition
(condition-slot-definition
525 effective-slot-definition
)
528 (defclass structure-direct-slot-definition
(structure-slot-definition
529 direct-slot-definition
)
532 (defclass structure-effective-slot-definition
(structure-slot-definition
533 effective-slot-definition
)
536 (defclass specializer
(metaobject)
537 ;; KLUDGE: in sbcl-0.9.10.2 this was renamed from TYPE, which was an
538 ;; external symbol of the CL package and hence potentially collides
539 ;; with user code. Renaming this to %TYPE, however, is the coward's
540 ;; way out, because the objects that PCL puts in this slot aren't
541 ;; (quite) types: they are closer to kinds of specializer. However,
542 ;; the wholesale renaming and disentangling of specializers didn't
543 ;; appeal. (See also message <sqd5hrclb2.fsf@cam.ac.uk> and
544 ;; responses in comp.lang.lisp). -- CSR, 2006-02-27
545 ((%type
:initform nil
:reader specializer-type
)))
547 ;;; STANDARD in this name doesn't mean "blessed by a standard" but
548 ;;; "comes as standard with PCL"; that is, it includes CLASS-EQ
549 ;;; and vestiges of PROTOTYPE specializers
550 (defclass standard-specializer
(specializer) ())
552 (defclass specializer-with-object
(specializer) ())
554 (defclass exact-class-specializer
(specializer) ())
556 (defclass class-eq-specializer
(standard-specializer
557 exact-class-specializer
558 specializer-with-object
)
559 ((object :initarg
:class
560 :reader specializer-class
561 :reader specializer-object
)))
563 (defclass class-prototype-specializer
(standard-specializer specializer-with-object
)
564 ((object :initarg
:class
565 :reader specializer-class
566 :reader specializer-object
)))
568 (defclass eql-specializer
(standard-specializer exact-class-specializer specializer-with-object
)
569 ((object :initarg
:object
:reader specializer-object
570 :reader eql-specializer-object
)))
572 (defvar *eql-specializer-table
* (make-hash-table :test
'eql
))
574 (defvar *eql-specializer-table-lock
*
575 (sb-thread::make-spinlock
:name
"EQL-specializer table lock"))
577 (defun intern-eql-specializer (object)
578 ;; Need to lock, so that two threads don't get non-EQ specializers
579 ;; for an EQL object.
580 (sb-thread::with-spinlock
(*eql-specializer-table-lock
*)
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
587 standard-specializer
)
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
)
609 ;; True if the class definition was compiled with a (SAFETY 3)
610 ;; optimization policy.
617 :reader class-finalized-p
)))
619 (def!method make-load-form
((class class
) &optional env
)
620 ;; FIXME: should we not instead pass ENV to FIND-CLASS? Probably
621 ;; doesn't matter while all our environments are the same...
622 (declare (ignore env
))
623 (let ((name (class-name class
)))
624 (unless (and name
(eq (find-class name nil
) class
))
625 (error "~@<Can't use anonymous or undefined class as constant: ~S~:@>"
627 `(find-class ',name
)))
629 ;;; The class PCL-CLASS is an implementation-specific common
630 ;;; superclass of all specified subclasses of the class CLASS.
631 (defclass pcl-class
(class)
632 ((%class-precedence-list
633 :reader class-precedence-list
)
634 ;; KLUDGE: see note in CPL-OR-NIL
636 :reader cpl-available-p
640 :reader class-can-precede-list
)
641 (incompatible-superclass-list
643 :accessor class-incompatible-superclass-list
)
646 :reader class-wrapper
)
649 :reader class-prototype
)))
651 (defclass slot-class
(pcl-class)
654 :reader class-direct-slots
)
657 :reader class-slots
)))
659 ;;; The class STD-CLASS is an implementation-specific common
660 ;;; superclass of the classes STANDARD-CLASS and
661 ;;; FUNCALLABLE-STANDARD-CLASS.
662 (defclass std-class
(slot-class)
665 (defclass standard-class
(std-class)
668 (defclass funcallable-standard-class
(std-class)
671 (defclass forward-referenced-class
(pcl-class) ())
673 (defclass built-in-class
(pcl-class) ())
675 (defclass condition-class
(slot-class) ())
677 (defclass structure-class
(slot-class)
680 :accessor class-defstruct-form
)
681 (defstruct-constructor
683 :accessor class-defstruct-constructor
)
686 :initarg
:from-defclass-p
)))
688 (defclass definition-source-mixin
(standard-object)
691 :reader definition-source
692 :initarg
:definition-source
)))
694 (defclass plist-mixin
(standard-object)
695 ((plist :initform
() :accessor object-plist
:initarg plist
)))
697 (defclass dependent-update-mixin
(plist-mixin) ())
699 (defparameter *early-class-predicates
*
700 '((specializer specializerp
)
701 (standard-specializer standard-specializer-p
)
702 (exact-class-specializer exact-class-specializer-p
)
703 (class-eq-specializer class-eq-specializer-p
)
704 (eql-specializer eql-specializer-p
)
706 (slot-class slot-class-p
)
707 (std-class std-class-p
)
708 (standard-class standard-class-p
)
709 (funcallable-standard-class funcallable-standard-class-p
)
710 (condition-class condition-class-p
)
711 (structure-class structure-class-p
)
712 (forward-referenced-class forward-referenced-class-p
)
714 (standard-method standard-method-p
)
715 (accessor-method accessor-method-p
)
716 (standard-accessor-method standard-accessor-method-p
)
717 (standard-reader-method standard-reader-method-p
)
718 (standard-writer-method standard-writer-method-p
)
719 (standard-boundp-method standard-boundp-method-p
)
720 (global-reader-method global-reader-method-p
)
721 (global-writer-method global-writer-method-p
)
722 (global-boundp-method global-boundp-method-p
)
723 (generic-function generic-function-p
)
724 (standard-generic-function standard-generic-function-p
)
725 (method-combination method-combination-p
)
726 (long-method-combination long-method-combination-p
)
727 (short-method-combination short-method-combination-p
)))