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