0.8.12.7: Merge package locks, AKA "what can go wrong with a 3783 line patch?"
[sbcl.git] / src / pcl / defclass.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 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
8 ;;;; information.
9
10 ;;;; copyright information from original PCL sources:
11 ;;;;
12 ;;;; Copyright (c) 1985, 1986, 1987, 1988, 1989, 1990 Xerox Corporation.
13 ;;;; All rights reserved.
14 ;;;;
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
18 ;;;; control laws.
19 ;;;;
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
22 ;;;; specification.
23
24 (in-package "SB-PCL")
25 \f
26 ;;;; DEFCLASS macro and close personal friends
27
28 ;;; ANSI says (Macro DEFCLASS, section 7.7) that DEFCLASS, if it
29 ;;; "appears as a top level form, the compiler must make the class
30 ;;; name be recognized as a valid type name in subsequent declarations
31 ;;; (as for deftype) and be recognized as a valid class name for
32 ;;; defmethod parameter specializers and for use as the :metaclass
33 ;;; option of a subsequent defclass."
34 (defun preinform-compiler-about-class-type (name)
35   ;; Unless the type system already has an actual type attached to
36   ;; NAME (in which case (1) writing a placeholder value over that
37   ;; actual type as a compile-time side-effect would probably be a bad
38   ;; idea and (2) anyway we don't need to modify it in order to make
39   ;; NAME be recognized as a valid type name)
40   (unless (info :type :kind name)
41     ;; Tell the compiler to expect a class with the given NAME, by
42     ;; writing a kind of minimal placeholder type information. This
43     ;; placeholder will be overwritten later when the class is defined.
44     (setf (info :type :kind name) :forthcoming-defclass-type))
45   (values))
46
47 ;;; state for the current DEFCLASS expansion
48 (defvar *initfunctions-for-this-defclass*)
49 (defvar *readers-for-this-defclass*)
50 (defvar *writers-for-this-defclass*)
51 (defvar *slot-names-for-this-defclass*)
52
53 ;;; Like the DEFMETHOD macro, the expansion of the DEFCLASS macro is
54 ;;; fixed. DEFCLASS always expands into a call to LOAD-DEFCLASS. Until
55 ;;; the meta-braid is set up, LOAD-DEFCLASS has a special definition
56 ;;; which simply collects all class definitions up, when the metabraid
57 ;;; is initialized it is done from those class definitions.
58 ;;;
59 ;;; After the metabraid has been setup, and the protocol for defining
60 ;;; classes has been defined, the real definition of LOAD-DEFCLASS is
61 ;;; installed by the file std-class.lisp
62 (defmacro defclass (&environment env name %direct-superclasses %direct-slots &rest %options)
63   (let ((supers  (copy-tree %direct-superclasses))
64         (slots   (copy-tree %direct-slots))
65         (options (copy-tree %options)))
66     (let ((metaclass 'standard-class))
67       (dolist (option options)
68         (if (not (listp option))
69           (error "~S is not a legal defclass option." option)
70           (when (eq (car option) :metaclass)
71             (unless (legal-class-name-p (cadr option))
72               (error "The value of the :metaclass option (~S) is not a~%~
73                       legal class name."
74                      (cadr option)))
75             (setq metaclass (cadr option))
76             (setf options (remove option options))
77             (return t))))
78
79       (let ((*initfunctions-for-this-defclass* ())
80             (*readers-for-this-defclass* ()) ;Truly a crock, but we got
81             (*writers-for-this-defclass* ()) ;to have it to live nicely.
82             (*slot-names-for-this-defclass* ()))
83         (let ((canonical-slots
84                 (mapcar (lambda (spec)
85                           (canonicalize-slot-specification name spec env))
86                         slots))
87               (other-initargs
88                 (mapcar (lambda (option)
89                           (canonicalize-defclass-option name option))
90                         options))
91               ;; DEFSTRUCT-P should be true if the class is defined
92               ;; with a metaclass STRUCTURE-CLASS, so that a DEFSTRUCT
93               ;; is compiled for the class.
94               (defstruct-p (and (eq *boot-state* 'complete)
95                                 (let ((mclass (find-class metaclass nil)))
96                                   (and mclass
97                                        (*subtypep
98                                         mclass
99                                         *the-class-structure-class*))))))
100           (let ((defclass-form
101                     `(progn
102                       (let ,(mapcar #'cdr *initfunctions-for-this-defclass*)
103                         (with-single-package-locked-error
104                             (:symbol ',name "defining ~A as a class")
105                           (%compiler-defclass ',name
106                                               ',*readers-for-this-defclass*
107                                               ',*writers-for-this-defclass*
108                                               ',*slot-names-for-this-defclass*)
109                           (load-defclass ',name
110                                          ',metaclass
111                                          ',supers
112                                          (list ,@canonical-slots)
113                                          (list ,@(apply #'append
114                                                         (when defstruct-p
115                                                           '(:from-defclass-p t))
116                                                         other-initargs))))))))
117             (if defstruct-p
118                 (progn
119                   ;; FIXME: (YUK!) Why do we do this? Because in order
120                   ;; to make the defstruct form, we need to know what
121                   ;; the accessors for the slots are, so we need
122                   ;; already to have hooked into the CLOS machinery.
123                   ;;
124                   ;; There may be a better way to do this: it would
125                   ;; involve knowing enough about PCL to ask "what
126                   ;; will my slot names and accessors be"; failing
127                   ;; this, we currently just evaluate the whole
128                   ;; kaboodle, and then use CLASS-DIRECT-SLOTS. --
129                   ;; CSR, 2002-06-07
130                   (eval defclass-form)
131                   (let* ((include (or (and supers
132                                            (fix-super (car supers)))
133                                       (and (not (eq name 'structure-object))
134                                            *the-class-structure-object*)))
135                          (defstruct-form (make-structure-class-defstruct-form
136                                           name (class-direct-slots (find-class name))
137                                           include)))
138                     `(progn
139                       (eval-when (:compile-toplevel :load-toplevel :execute)
140                         ,defstruct-form) ; really compile the defstruct-form
141                       (eval-when (:compile-toplevel :load-toplevel :execute)
142                         ,defclass-form))))
143                 `(progn
144                    ;; By telling the type system at compile time about
145                    ;; the existence of a class named NAME, we can avoid
146                    ;; various bogus warnings about "type isn't defined yet"
147                    ;; for code elsewhere in the same file which uses
148                    ;; the name of the type.
149                    ;;
150                    ;; We only need to do this at compile time, because
151                    ;; at load and execute time we write the actual
152                    ;; full-blown class, so the "a class of this name is
153                    ;; coming" note we write here would be irrelevant.
154                    (eval-when (:compile-toplevel)
155                      (%compiler-defclass ',name
156                                          ',*readers-for-this-defclass*
157                                          ',*writers-for-this-defclass*
158                                          ',*slot-names-for-this-defclass*))
159                    (eval-when (:load-toplevel :execute)
160                      ,defclass-form)))))))))
161
162 (defun %compiler-defclass (name readers writers slot-names)
163   (with-single-package-locked-error (:symbol name "defining ~A as a class")
164     (preinform-compiler-about-class-type name)
165     (proclaim `(ftype (function (t) t)
166                 ,@readers
167                 ,@(mapcar #'slot-reader-name slot-names)
168                 ,@(mapcar #'slot-boundp-name slot-names)))
169     (proclaim `(ftype (function (t t) t)
170                 ,@writers ,@(mapcar #'slot-writer-name slot-names)))))
171
172 (defun make-initfunction (initform)
173   (cond ((or (eq initform t)
174              (equal initform ''t))
175          '(function constantly-t))
176         ((or (eq initform nil)
177              (equal initform ''nil))
178          '(function constantly-nil))
179         ((or (eql initform 0)
180              (equal initform ''0))
181          '(function constantly-0))
182         (t
183          (let ((entry (assoc initform *initfunctions-for-this-defclass*
184                              :test #'equal)))
185            (unless entry
186              (setq entry (list initform
187                                (gensym)
188                                `(function (lambda () ,initform))))
189              (push entry *initfunctions-for-this-defclass*))
190            (cadr entry)))))
191
192 (defun canonicalize-slot-specification (class-name spec env)
193   (labels ((slot-name-illegal (reason)
194              (error 'simple-program-error
195                     :format-control
196                     (format nil "~~@<in DEFCLASS ~~S, the slot name in the ~
197                                  specification ~~S is ~A.~~@:>" reason)
198                     :format-arguments (list class-name spec)))
199            (check-slot-name-legality (name)
200              (cond
201                ((not (symbolp name))
202                 (slot-name-illegal "not a symbol"))
203                ((keywordp name)
204                 (slot-name-illegal "a keyword"))
205                ((constantp name env)
206                 (slot-name-illegal "a constant")))))
207     (cond ((atom spec)
208            (check-slot-name-legality spec)
209            (push spec *slot-names-for-this-defclass*)
210            `'(:name ,spec))
211           ((null (cdr spec))
212            (check-slot-name-legality (car spec))
213            (push (car spec) *slot-names-for-this-defclass*)
214          `'(:name ,(car spec)))
215           ((null (cddr spec))
216            (error 'simple-program-error
217                   :format-control
218                   "~@<in DEFCLASS ~S, the slot specification ~S is invalid; ~
219                    the probable intended meaning may be achieved by ~
220                    specifiying ~S instead.~>"
221                   :format-arguments
222                   (list class-name spec
223                         `(,(car spec) :initform ,(cadr spec)))))
224           (t
225            (let* ((name (car spec))
226                   (spec (cdr spec))
227                   (readers ())
228                   (writers ())
229                   (initargs ())
230                   (unsupplied (list nil))
231                   (initform (getf spec :initform unsupplied)))
232              (check-slot-name-legality name)
233              (push name *slot-names-for-this-defclass*)
234              (doplist (key val) spec
235                (case key
236                  (:accessor (push val readers)
237                             (push `(setf ,val) writers))
238                  (:reader   (push val readers))
239                  (:writer   (push val writers))
240                  (:initarg  (push val initargs))))
241              (loop (unless (remf spec :accessor) (return)))
242              (loop (unless (remf spec :reader)   (return)))
243              (loop (unless (remf spec :writer)   (return)))
244              (loop (unless (remf spec :initarg)  (return)))
245              (setq *writers-for-this-defclass*
246                    (append writers *writers-for-this-defclass*))
247              (setq *readers-for-this-defclass*
248                    (append readers *readers-for-this-defclass*))
249              (setq spec `(:name     ',name
250                           :readers  ',readers
251                           :writers  ',writers
252                           :initargs ',initargs
253                           ',spec))
254              (if (eq initform unsupplied)
255                  `(list* ,@spec)
256                  `(list* :initfunction ,(make-initfunction initform)
257                          ,@spec)))))))
258
259 (defun canonicalize-defclass-option (class-name option)
260   (declare (ignore class-name))
261   (case (car option)
262     (:default-initargs
263       (let ((canonical ()))
264         (let (key val (tail (cdr option)))
265           (loop (when (null tail) (return nil))
266                 (setq key (pop tail)
267                       val (pop tail))
268                 (push ``(,',key ,,(make-initfunction val) ,',val) canonical))
269           `(:direct-default-initargs (list ,@(nreverse canonical))))))
270     (:documentation
271       `(',(car option) ',(cadr option)))
272     (otherwise
273      `(',(car option) ',(cdr option)))))
274 \f
275 ;;; This is the early definition of LOAD-DEFCLASS. It just collects up
276 ;;; all the class definitions in a list. Later, in braid1.lisp, these
277 ;;; are actually defined.
278
279 ;;; Each entry in *EARLY-CLASS-DEFINITIONS* is an EARLY-CLASS-DEFINITION.
280 (defparameter *early-class-definitions* ())
281
282 (defun early-class-definition (class-name)
283   (or (find class-name *early-class-definitions* :key #'ecd-class-name)
284       (error "~S is not a class in *early-class-definitions*." class-name)))
285
286 (defun make-early-class-definition
287        (name source metaclass
288         superclass-names canonical-slots other-initargs)
289   (list 'early-class-definition
290         name source metaclass
291         superclass-names canonical-slots other-initargs))
292
293 (defun ecd-class-name        (ecd) (nth 1 ecd))
294 (defun ecd-source            (ecd) (nth 2 ecd))
295 (defun ecd-metaclass         (ecd) (nth 3 ecd))
296 (defun ecd-superclass-names  (ecd) (nth 4 ecd))
297 (defun ecd-canonical-slots   (ecd) (nth 5 ecd))
298 (defun ecd-other-initargs    (ecd) (nth 6 ecd))
299
300 (defvar *early-class-slots* nil)
301
302 (defun canonical-slot-name (canonical-slot)
303   (getf canonical-slot :name))
304
305 (defun early-class-slots (class-name)
306   (cdr (or (assoc class-name *early-class-slots*)
307            (let ((a (cons class-name
308                           (mapcar #'canonical-slot-name
309                                   (early-collect-inheritance class-name)))))
310              (push a *early-class-slots*)
311              a))))
312
313 (defun early-class-size (class-name)
314   (length (early-class-slots class-name)))
315
316 (defun early-collect-inheritance (class-name)
317   ;;(declare (values slots cpl default-initargs direct-subclasses))
318   (let ((cpl (early-collect-cpl class-name)))
319     (values (early-collect-slots cpl)
320             cpl
321             (early-collect-default-initargs cpl)
322             (let (collect)
323               (dolist (definition *early-class-definitions*)
324                 (when (memq class-name (ecd-superclass-names definition))
325                   (push (ecd-class-name definition) collect)))
326               (nreverse collect)))))
327
328 (defun early-collect-slots (cpl)
329   (let* ((definitions (mapcar #'early-class-definition cpl))
330          (super-slots (mapcar #'ecd-canonical-slots definitions))
331          (slots (apply #'append (reverse super-slots))))
332     (dolist (s1 slots)
333       (let ((name1 (canonical-slot-name s1)))
334         (dolist (s2 (cdr (memq s1 slots)))
335           (when (eq name1 (canonical-slot-name s2))
336             (error "More than one early class defines a slot with the~%~
337                     name ~S. This can't work because the bootstrap~%~
338                     object system doesn't know how to compute effective~%~
339                     slots."
340                    name1)))))
341     slots))
342
343 (defun early-collect-cpl (class-name)
344   (labels ((walk (c)
345              (let* ((definition (early-class-definition c))
346                     (supers (ecd-superclass-names definition)))
347                (cons c
348                      (apply #'append (mapcar #'early-collect-cpl supers))))))
349     (remove-duplicates (walk class-name) :from-end nil :test #'eq)))
350
351 (defun early-collect-default-initargs (cpl)
352   (let ((default-initargs ()))
353     (dolist (class-name cpl)
354       (let* ((definition (early-class-definition class-name))
355              (others (ecd-other-initargs definition)))
356         (loop (when (null others) (return nil))
357               (let ((initarg (pop others)))
358                 (unless (eq initarg :direct-default-initargs)
359                  (error "~@<The defclass option ~S is not supported by ~
360                         the bootstrap object system.~:@>"
361                         initarg)))
362               (setq default-initargs
363                     (nconc default-initargs (reverse (pop others)))))))
364     (reverse default-initargs)))
365
366 (defun !bootstrap-slot-index (class-name slot-name)
367   (or (position slot-name (early-class-slots class-name))
368       (error "~S not found" slot-name)))
369
370 ;;; !BOOTSTRAP-GET-SLOT and !BOOTSTRAP-SET-SLOT are used to access and
371 ;;; change the values of slots during bootstrapping. During
372 ;;; bootstrapping, there are only two kinds of objects whose slots we
373 ;;; need to access, CLASSes and SLOT-DEFINITIONs. The first argument
374 ;;; to these functions tells whether the object is a CLASS or a
375 ;;; SLOT-DEFINITION.
376 ;;;
377 ;;; Note that the way this works it stores the slot in the same place
378 ;;; in memory that the full object system will expect to find it
379 ;;; later. This is critical to the bootstrapping process, the whole
380 ;;; changeover to the full object system is predicated on this.
381 ;;;
382 ;;; One important point is that the layout of standard classes and
383 ;;; standard slots must be computed the same way in this file as it is
384 ;;; by the full object system later.
385 (defmacro !bootstrap-get-slot (type object slot-name)
386   `(clos-slots-ref (get-slots ,object)
387                    (!bootstrap-slot-index ,type ,slot-name)))
388 (defun !bootstrap-set-slot (type object slot-name new-value)
389   (setf (!bootstrap-get-slot type object slot-name) new-value))
390
391 (defun early-class-name (class)
392   (!bootstrap-get-slot 'class class 'name))
393
394 (defun early-class-precedence-list (class)
395   (!bootstrap-get-slot 'pcl-class class 'class-precedence-list))
396
397 (defun early-class-name-of (instance)
398   (early-class-name (class-of instance)))
399
400 (defun early-class-slotds (class)
401   (!bootstrap-get-slot 'slot-class class 'slots))
402
403 (defun early-slot-definition-name (slotd)
404   (!bootstrap-get-slot 'standard-effective-slot-definition slotd 'name))
405
406 (defun early-slot-definition-location (slotd)
407   (!bootstrap-get-slot 'standard-effective-slot-definition slotd 'location))
408
409 (defun early-accessor-method-slot-name (method)
410   (!bootstrap-get-slot 'standard-accessor-method method 'slot-name))
411
412 (unless (fboundp 'class-name-of)
413   (setf (symbol-function 'class-name-of)
414         (symbol-function 'early-class-name-of)))
415 (unintern 'early-class-name-of)
416
417 (defun early-class-direct-subclasses (class)
418   (!bootstrap-get-slot 'class class 'direct-subclasses))
419
420 (declaim (notinline load-defclass))
421 (defun load-defclass (name metaclass supers canonical-slots canonical-options)
422   (setq supers  (copy-tree supers)
423         canonical-slots   (copy-tree canonical-slots)
424         canonical-options (copy-tree canonical-options))
425   (let ((ecd
426           (make-early-class-definition name
427                                        *load-pathname*
428                                        metaclass
429                                        supers
430                                        canonical-slots
431                                        canonical-options))
432         (existing
433           (find name *early-class-definitions* :key #'ecd-class-name)))
434     (setq *early-class-definitions*
435           (cons ecd (remove existing *early-class-definitions*)))
436     ecd))
437