0.8.0.8:
[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                      ,@(mapcar (lambda (x)
103                                  `(declaim (ftype (function (t) t) ,x)))
104                                *readers-for-this-defclass*)
105                      ,@(mapcar (lambda (x)
106                                  `(declaim (ftype (function (t t) t) ,x)))
107                                *writers-for-this-defclass*)
108                      ,@(mapcar (lambda (x)
109                                  `(declaim (ftype (function (t) t)
110                                                   ,(slot-reader-name x)
111                                                   ,(slot-boundp-name x))
112                                            (ftype (function (t t) t)
113                                                   ,(slot-writer-name x))))
114                                *slot-names-for-this-defclass*)
115                      (let ,(mapcar #'cdr *initfunctions-for-this-defclass*)
116                        (load-defclass ',name
117                                       ',metaclass
118                                       ',supers
119                                       (list ,@canonical-slots)
120                                       (list ,@(apply #'append
121                                                      (when defstruct-p
122                                                        '(:from-defclass-p t))
123                                                      other-initargs)))))))
124             (if defstruct-p
125                 (progn
126                   ;; FIXME: (YUK!) Why do we do this? Because in order
127                   ;; to make the defstruct form, we need to know what
128                   ;; the accessors for the slots are, so we need
129                   ;; already to have hooked into the CLOS machinery.
130                   ;;
131                   ;; There may be a better way to do this: it would
132                   ;; involve knowing enough about PCL to ask "what
133                   ;; will my slot names and accessors be"; failing
134                   ;; this, we currently just evaluate the whole
135                   ;; kaboodle, and then use CLASS-DIRECT-SLOTS. --
136                   ;; CSR, 2002-06-07
137                   (eval defclass-form)
138                   (let* ((include (or (and supers
139                                            (fix-super (car supers)))
140                                       (and (not (eq name 'structure-object))
141                                            *the-class-structure-object*)))
142                          (defstruct-form (make-structure-class-defstruct-form
143                                           name (class-direct-slots (find-class name)) include)))
144                     `(progn
145                       (eval-when (:compile-toplevel :load-toplevel :execute)
146                         ,defstruct-form) ; really compile the defstruct-form
147                       (eval-when (:compile-toplevel :load-toplevel :execute)
148                         ,defclass-form))))
149                 `(progn
150                    ;; By telling the type system at compile time about
151                    ;; the existence of a class named NAME, we can avoid
152                    ;; various bogus warnings about "type isn't defined yet"
153                    ;; for code elsewhere in the same file which uses
154                    ;; the name of the type.
155                    ;;
156                    ;; We only need to do this at compile time, because
157                    ;; at load and execute time we write the actual
158                    ;; full-blown class, so the "a class of this name is
159                    ;; coming" note we write here would be irrelevant.
160                    (eval-when (:compile-toplevel)
161                      (preinform-compiler-about-class-type ',name))
162                    ,defclass-form))))))))
163
164 (defun make-initfunction (initform)
165   (cond ((or (eq initform t)
166              (equal initform ''t))
167          '(function constantly-t))
168         ((or (eq initform nil)
169              (equal initform ''nil))
170          '(function constantly-nil))
171         ((or (eql initform 0)
172              (equal initform ''0))
173          '(function constantly-0))
174         (t
175          (let ((entry (assoc initform *initfunctions-for-this-defclass*
176                              :test #'equal)))
177            (unless entry
178              (setq entry (list initform
179                                (gensym)
180                                `(function (lambda () ,initform))))
181              (push entry *initfunctions-for-this-defclass*))
182            (cadr entry)))))
183
184 (defun canonicalize-slot-specification (class-name spec env)
185   (labels ((slot-name-illegal (reason)
186              (error 'simple-program-error
187                     :format-control
188                     (format nil "~~@<in DEFCLASS ~~S, the slot name in the ~
189                                  specification ~~S is ~A.~~@:>" reason)
190                     :format-arguments (list class-name spec)))
191            (check-slot-name-legality (name)
192              (cond
193                ((not (symbolp name))
194                 (slot-name-illegal "not a symbol"))
195                ((keywordp name)
196                 (slot-name-illegal "a keyword"))
197                ((constantp name env)
198                 (slot-name-illegal "a constant")))))
199     (cond ((atom spec)
200            (check-slot-name-legality spec)
201            (push spec *slot-names-for-this-defclass*)
202            `'(:name ,spec))
203           ((null (cdr spec))
204            (check-slot-name-legality (car spec))
205            (push (car spec) *slot-names-for-this-defclass*)
206          `'(:name ,(car spec)))
207           ((null (cddr spec))
208            (error 'simple-program-error
209                   :format-control
210                   "~@<in DEFCLASS ~S, the slot specification ~S is invalid; ~
211                    the probable intended meaning may be achieved by ~
212                    specifiying ~S instead."
213                   :format-arguments
214                   (list class-name spec
215                         `(,(car spec) :initform ,(cadr spec)))))
216           (t
217            (let* ((name (car spec))
218                   (spec (cdr spec))
219                   (readers ())
220                   (writers ())
221                   (initargs ())
222                   (unsupplied (list nil))
223                   (initform (getf spec :initform unsupplied)))
224              (check-slot-name-legality name)
225              (push name *slot-names-for-this-defclass*)
226              (doplist (key val) spec
227                (case key
228                  (:accessor (push val readers)
229                             (push `(setf ,val) writers))
230                  (:reader   (push val readers))
231                  (:writer   (push val writers))
232                  (:initarg  (push val initargs))))
233              (loop (unless (remf spec :accessor) (return)))
234              (loop (unless (remf spec :reader)   (return)))
235              (loop (unless (remf spec :writer)   (return)))
236              (loop (unless (remf spec :initarg)  (return)))
237              (setq *writers-for-this-defclass*
238                    (append writers *writers-for-this-defclass*))
239              (setq *readers-for-this-defclass*
240                    (append readers *readers-for-this-defclass*))
241              (setq spec `(:name     ',name
242                           :readers  ',readers
243                           :writers  ',writers
244                           :initargs ',initargs
245                           ',spec))
246              (if (eq initform unsupplied)
247                  `(list* ,@spec)
248                  `(list* :initfunction ,(make-initfunction initform)
249                          ,@spec)))))))
250
251 (defun canonicalize-defclass-option (class-name option)
252   (declare (ignore class-name))
253   (case (car option)
254     (:default-initargs
255       (let ((canonical ()))
256         (let (key val (tail (cdr option)))
257           (loop (when (null tail) (return nil))
258                 (setq key (pop tail)
259                       val (pop tail))
260                 (push ``(,',key ,,(make-initfunction val) ,',val) canonical))
261           `(:direct-default-initargs (list ,@(nreverse canonical))))))
262     (:documentation
263       `(',(car option) ',(cadr option)))
264     (otherwise
265      `(',(car option) ',(cdr option)))))
266 \f
267 ;;; This is the early definition of LOAD-DEFCLASS. It just collects up
268 ;;; all the class definitions in a list. Later, in braid1.lisp, these
269 ;;; are actually defined.
270
271 ;;; Each entry in *EARLY-CLASS-DEFINITIONS* is an EARLY-CLASS-DEFINITION.
272 (defparameter *early-class-definitions* ())
273
274 (defun early-class-definition (class-name)
275   (or (find class-name *early-class-definitions* :key #'ecd-class-name)
276       (error "~S is not a class in *early-class-definitions*." class-name)))
277
278 (defun make-early-class-definition
279        (name source metaclass
280         superclass-names canonical-slots other-initargs)
281   (list 'early-class-definition
282         name source metaclass
283         superclass-names canonical-slots other-initargs))
284
285 (defun ecd-class-name        (ecd) (nth 1 ecd))
286 (defun ecd-source            (ecd) (nth 2 ecd))
287 (defun ecd-metaclass         (ecd) (nth 3 ecd))
288 (defun ecd-superclass-names  (ecd) (nth 4 ecd))
289 (defun ecd-canonical-slots   (ecd) (nth 5 ecd))
290 (defun ecd-other-initargs    (ecd) (nth 6 ecd))
291
292 (defvar *early-class-slots* nil)
293
294 (defun canonical-slot-name (canonical-slot)
295   (getf canonical-slot :name))
296
297 (defun early-class-slots (class-name)
298   (cdr (or (assoc class-name *early-class-slots*)
299            (let ((a (cons class-name
300                           (mapcar #'canonical-slot-name
301                                   (early-collect-inheritance class-name)))))
302              (push a *early-class-slots*)
303              a))))
304
305 (defun early-class-size (class-name)
306   (length (early-class-slots class-name)))
307
308 (defun early-collect-inheritance (class-name)
309   ;;(declare (values slots cpl default-initargs direct-subclasses))
310   (let ((cpl (early-collect-cpl class-name)))
311     (values (early-collect-slots cpl)
312             cpl
313             (early-collect-default-initargs cpl)
314             (let (collect)
315               (dolist (definition *early-class-definitions*)
316                 (when (memq class-name (ecd-superclass-names definition))
317                   (push (ecd-class-name definition) collect)))
318               (nreverse collect)))))
319
320 (defun early-collect-slots (cpl)
321   (let* ((definitions (mapcar #'early-class-definition cpl))
322          (super-slots (mapcar #'ecd-canonical-slots definitions))
323          (slots (apply #'append (reverse super-slots))))
324     (dolist (s1 slots)
325       (let ((name1 (canonical-slot-name s1)))
326         (dolist (s2 (cdr (memq s1 slots)))
327           (when (eq name1 (canonical-slot-name s2))
328             (error "More than one early class defines a slot with the~%~
329                     name ~S. This can't work because the bootstrap~%~
330                     object system doesn't know how to compute effective~%~
331                     slots."
332                    name1)))))
333     slots))
334
335 (defun early-collect-cpl (class-name)
336   (labels ((walk (c)
337              (let* ((definition (early-class-definition c))
338                     (supers (ecd-superclass-names definition)))
339                (cons c
340                      (apply #'append (mapcar #'early-collect-cpl supers))))))
341     (remove-duplicates (walk class-name) :from-end nil :test #'eq)))
342
343 (defun early-collect-default-initargs (cpl)
344   (let ((default-initargs ()))
345     (dolist (class-name cpl)
346       (let* ((definition (early-class-definition class-name))
347              (others (ecd-other-initargs definition)))
348         (loop (when (null others) (return nil))
349               (let ((initarg (pop others)))
350                 (unless (eq initarg :direct-default-initargs)
351                  (error "~@<The defclass option ~S is not supported by ~
352                         the bootstrap object system.~:@>"
353                         initarg)))
354               (setq default-initargs
355                     (nconc default-initargs (reverse (pop others)))))))
356     (reverse default-initargs)))
357
358 (defun !bootstrap-slot-index (class-name slot-name)
359   (or (position slot-name (early-class-slots class-name))
360       (error "~S not found" slot-name)))
361
362 ;;; !BOOTSTRAP-GET-SLOT and !BOOTSTRAP-SET-SLOT are used to access and
363 ;;; change the values of slots during bootstrapping. During
364 ;;; bootstrapping, there are only two kinds of objects whose slots we
365 ;;; need to access, CLASSes and SLOT-DEFINITIONs. The first argument
366 ;;; to these functions tells whether the object is a CLASS or a
367 ;;; SLOT-DEFINITION.
368 ;;;
369 ;;; Note that the way this works it stores the slot in the same place
370 ;;; in memory that the full object system will expect to find it
371 ;;; later. This is critical to the bootstrapping process, the whole
372 ;;; changeover to the full object system is predicated on this.
373 ;;;
374 ;;; One important point is that the layout of standard classes and
375 ;;; standard slots must be computed the same way in this file as it is
376 ;;; by the full object system later.
377 (defmacro !bootstrap-get-slot (type object slot-name)
378   `(clos-slots-ref (get-slots ,object)
379                    (!bootstrap-slot-index ,type ,slot-name)))
380 (defun !bootstrap-set-slot (type object slot-name new-value)
381   (setf (!bootstrap-get-slot type object slot-name) new-value))
382
383 (defun early-class-name (class)
384   (!bootstrap-get-slot 'class class 'name))
385
386 (defun early-class-precedence-list (class)
387   (!bootstrap-get-slot 'pcl-class class 'class-precedence-list))
388
389 (defun early-class-name-of (instance)
390   (early-class-name (class-of instance)))
391
392 (defun early-class-slotds (class)
393   (!bootstrap-get-slot 'slot-class class 'slots))
394
395 (defun early-slot-definition-name (slotd)
396   (!bootstrap-get-slot 'standard-effective-slot-definition slotd 'name))
397
398 (defun early-slot-definition-location (slotd)
399   (!bootstrap-get-slot 'standard-effective-slot-definition slotd 'location))
400
401 (defun early-accessor-method-slot-name (method)
402   (!bootstrap-get-slot 'standard-accessor-method method 'slot-name))
403
404 (unless (fboundp 'class-name-of)
405   (setf (symbol-function 'class-name-of)
406         (symbol-function 'early-class-name-of)))
407 (unintern 'early-class-name-of)
408
409 (defun early-class-direct-subclasses (class)
410   (!bootstrap-get-slot 'class class 'direct-subclasses))
411
412 (declaim (notinline load-defclass))
413 (defun load-defclass (name metaclass supers canonical-slots canonical-options)
414   (setq supers  (copy-tree supers)
415         canonical-slots   (copy-tree canonical-slots)
416         canonical-options (copy-tree canonical-options))
417   (let ((ecd
418           (make-early-class-definition name
419                                        *load-pathname*
420                                        metaclass
421                                        supers
422                                        canonical-slots
423                                        canonical-options))
424         (existing
425           (find name *early-class-definitions* :key #'ecd-class-name)))
426     (setq *early-class-definitions*
427           (cons ecd (remove existing *early-class-definitions*)))
428     ecd))
429