0.6.12.25:
[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
27 (defun make-progn (&rest forms)
28   (let ((progn-form nil))
29     (labels ((collect-forms (forms)
30                (unless (null forms)
31                  (collect-forms (cdr forms))
32                  (if (and (listp (car forms))
33                           (eq (caar forms) 'progn))
34                      (collect-forms (cdar forms))
35                      (push (car forms) progn-form)))))
36       (collect-forms forms)
37       (cons 'progn progn-form))))
38 \f
39 ;;; Like the DEFMETHOD macro, the expansion of the DEFCLASS macro is
40 ;;; fixed. DEFCLASS always expands into a call to LOAD-DEFCLASS. Until
41 ;;; the meta-braid is set up, LOAD-DEFCLASS has a special definition
42 ;;; which simply collects all class definitions up, when the metabraid
43 ;;; is initialized it is done from those class definitions.
44 ;;;
45 ;;; After the metabraid has been setup, and the protocol for defining
46 ;;; classes has been defined, the real definition of LOAD-DEFCLASS is
47 ;;; installed by the file defclass.lisp
48 (defmacro defclass (name direct-superclasses direct-slots &rest options)
49   (expand-defclass name direct-superclasses direct-slots options))
50
51 (defun expand-defclass (name supers slots options)
52   (setq supers  (copy-tree supers)
53         slots   (copy-tree slots)
54         options (copy-tree options))
55   (let ((metaclass 'standard-class))
56     (dolist (option options)
57       (if (not (listp option))
58           (error "~S is not a legal defclass option." option)
59           (when (eq (car option) ':metaclass)
60             (unless (legal-class-name-p (cadr option))
61               (error "The value of the :metaclass option (~S) is not a~%~
62                       legal class name."
63                      (cadr option)))
64             (setq metaclass
65                   (case (cadr option)
66                     (cl:standard-class 'standard-class)
67                     (cl:structure-class 'structure-class)
68                     (t (cadr option))))
69             (setf options (remove option options))
70             (return t))))
71
72     (let ((*initfunctions* ())
73           (*readers* ())                ;Truly a crock, but we got
74           (*writers* ()))               ;to have it to live nicely.
75       (declare (special *initfunctions* *readers* *writers*))
76       (let ((canonical-slots
77               (mapcar #'(lambda (spec)
78                           (canonicalize-slot-specification name spec))
79                       slots))
80             (other-initargs
81               (mapcar #'(lambda (option)
82                           (canonicalize-defclass-option name option))
83                       options))
84             (defstruct-p (and (eq *boot-state* 'complete)
85                               (let ((mclass (find-class metaclass nil)))
86                                 (and mclass
87                                      (*subtypep
88                                       mclass
89                                       *the-class-structure-class*))))))
90         (let ((defclass-form
91                 `(progn
92                    ,@(mapcar (lambda (x)
93                                `(declaim (ftype (function (t) t) ,x)))
94                              *readers*)
95                    ,@(mapcar (lambda (x)
96                                `(declaim (ftype (function (t t) t) ,x)))
97                              *writers*)
98                    (let ,(mapcar #'cdr *initfunctions*)
99                      (load-defclass ',name
100                                     ',metaclass
101                                     ',supers
102                                     (list ,@canonical-slots)
103                                     (list ,@(apply #'append
104                                                    (when defstruct-p
105                                                      '(:from-defclass-p t))
106                                                    other-initargs)))))))
107           ;; FIXME: The way that we do things like (EVAL DEFCLASS-FORM)
108           ;; here is un-ANSI-Common-Lisp-y and leads to problems
109           ;; (like DEFUN for the type predicate being called more than
110           ;; once when we do DEFCLASS at the interpreter prompt),
111           ;; causing bogus style warnings. It would be better to
112           ;; rewrite this so that the macroexpansion looks like e.g.
113           ;; (PROGN
114           ;;   (EVAL-WHEN (:COMPILE-TOPLEVEL :LOAD-TOPLEVEL :EXECUTE)
115           ;;     (FROB1 ..))
116           ;;   (EVAL-WHEN (:LOAD-TOPLEVEL :EXECUTE)
117           ;;     (FROB2 ..)))
118           (if defstruct-p
119               (progn
120                 (eval defclass-form) ; Define the class now, so that..
121                 `(progn       ; ..the defstruct can be compiled.
122                    ,(class-defstruct-form (find-class name))
123                    ,defclass-form))
124               (progn
125                 (when (eq *boot-state* 'complete)
126                   ;; FIXME: MNA (on sbcl-devel 2001-05-30) reported
127                   ;; (if I understand correctly -- WHN) that this call
128                   ;; is directly responsible for defining
129                   ;; class-predicates which always return
130                   ;; CONSTANTLY-NIL in the compile-time environment,
131                   ;; and is indirectly responsible for bogus warnings
132                   ;; about redefinitions when making definitions in
133                   ;; the interpreter. I didn't like his fix (deleting
134                   ;; the call) since I think the type system *should*
135                   ;; be informed about class definitions here. And I'm
136                   ;; not eager to look too deeply into this sort of
137                   ;; done-too-many-times-in-the-interpreter problem
138                   ;; right now, since it should be easier to make a
139                   ;; clean fix when EVAL-WHEN is made more ANSI (as
140                   ;; per the IR1 section in the BUGS file). But
141                   ;; at some point this should be cleaned up.
142                   (inform-type-system-about-std-class name))
143                 defclass-form)))))))
144
145 (defun make-initfunction (initform)
146   (declare (special *initfunctions*))
147   (cond ((or (eq initform t)
148              (equal initform ''t))
149          '(function constantly-t))
150         ((or (eq initform nil)
151              (equal initform ''nil))
152          '(function constantly-nil))
153         ((or (eql initform 0)
154              (equal initform ''0))
155          '(function constantly-0))
156         (t
157          (let ((entry (assoc initform *initfunctions* :test #'equal)))
158            (unless entry
159              (setq entry (list initform
160                                (gensym)
161                                `(function (lambda () ,initform))))
162              (push entry *initfunctions*))
163            (cadr entry)))))
164
165 (defun canonicalize-slot-specification (class-name spec)
166   (declare (special *readers* *writers*))
167   (cond ((and (symbolp spec)
168               (not (keywordp spec))
169               (not (memq spec '(t nil))))
170          `'(:name ,spec))
171         ((not (consp spec))
172          (error "~S is not a legal slot specification." spec))
173         ((null (cdr spec))
174          `'(:name ,(car spec)))
175         ((null (cddr spec))
176          (error "In DEFCLASS ~S, the slot specification ~S is obsolete.~%~
177                  Convert it to ~S"
178                 class-name spec (list (car spec) :initform (cadr spec))))
179         (t
180          (let* ((name (pop spec))
181                 (readers ())
182                 (writers ())
183                 (initargs ())
184                 (unsupplied (list nil))
185                 (initform (getf spec :initform unsupplied)))
186            (doplist (key val) spec
187              (case key
188                (:accessor (push val readers)
189                           (push `(setf ,val) writers))
190                (:reader   (push val readers))
191                (:writer   (push val writers))
192                (:initarg  (push val initargs))))
193            (loop (unless (remf spec :accessor) (return)))
194            (loop (unless (remf spec :reader)   (return)))
195            (loop (unless (remf spec :writer)   (return)))
196            (loop (unless (remf spec :initarg)  (return)))
197            (setq *writers* (append writers *writers*))
198            (setq *readers* (append readers *readers*))
199            (setq spec `(:name     ',name
200                         :readers  ',readers
201                         :writers  ',writers
202                         :initargs ',initargs
203                         ',spec))
204            (if (eq initform unsupplied)
205                `(list* ,@spec)
206                `(list* :initfunction ,(make-initfunction initform) ,@spec))))))
207                                                 
208 (defun canonicalize-defclass-option (class-name option)
209   (declare (ignore class-name))
210   (case (car option)
211     (:default-initargs
212       (let ((canonical ()))
213         (let (key val (tail (cdr option)))
214           (loop (when (null tail) (return nil))
215                 (setq key (pop tail)
216                       val (pop tail))
217                 (push ``(,',key ,,(make-initfunction val) ,',val) canonical))
218           `(':direct-default-initargs (list ,@(nreverse canonical))))))
219     (:documentation
220       `(',(car option) ',(cadr option)))
221     (otherwise
222      `(',(car option) ',(cdr option)))))
223 \f
224 ;;; This is the early definition of load-defclass. It just collects up
225 ;;; all the class definitions in a list. Later, in the file
226 ;;; braid1.lisp, these are actually defined.
227
228 ;;; Each entry in *EARLY-CLASS-DEFINITIONS* is an EARLY-CLASS-DEFINITION.
229 (defparameter *early-class-definitions* ())
230
231 (defun early-class-definition (class-name)
232   (or (find class-name *early-class-definitions* :key #'ecd-class-name)
233       (error "~S is not a class in *early-class-definitions*." class-name)))
234
235 (defun make-early-class-definition
236        (name source metaclass
237         superclass-names canonical-slots other-initargs)
238   (list 'early-class-definition
239         name source metaclass
240         superclass-names canonical-slots other-initargs))
241
242 (defun ecd-class-name        (ecd) (nth 1 ecd))
243 (defun ecd-source            (ecd) (nth 2 ecd))
244 (defun ecd-metaclass         (ecd) (nth 3 ecd))
245 (defun ecd-superclass-names  (ecd) (nth 4 ecd))
246 (defun ecd-canonical-slots   (ecd) (nth 5 ecd))
247 (defun ecd-other-initargs    (ecd) (nth 6 ecd))
248
249 (defvar *early-class-slots* nil)
250
251 (defun canonical-slot-name (canonical-slot)
252   (getf canonical-slot :name))
253
254 (defun early-class-slots (class-name)
255   (cdr (or (assoc class-name *early-class-slots*)
256            (let ((a (cons class-name
257                           (mapcar #'canonical-slot-name
258                                   (early-collect-inheritance class-name)))))
259              (push a *early-class-slots*)
260              a))))
261
262 (defun early-class-size (class-name)
263   (length (early-class-slots class-name)))
264
265 (defun early-collect-inheritance (class-name)
266   ;;(declare (values slots cpl default-initargs direct-subclasses))
267   (let ((cpl (early-collect-cpl class-name)))
268     (values (early-collect-slots cpl)
269             cpl
270             (early-collect-default-initargs cpl)
271             (gathering1 (collecting)
272               (dolist (definition *early-class-definitions*)
273                 (when (memq class-name (ecd-superclass-names definition))
274                   (gather1 (ecd-class-name definition))))))))
275
276 (defun early-collect-slots (cpl)
277   (let* ((definitions (mapcar #'early-class-definition cpl))
278          (super-slots (mapcar #'ecd-canonical-slots definitions))
279          (slots (apply #'append (reverse super-slots))))
280     (dolist (s1 slots)
281       (let ((name1 (canonical-slot-name s1)))
282         (dolist (s2 (cdr (memq s1 slots)))
283           (when (eq name1 (canonical-slot-name s2))
284             (error "More than one early class defines a slot with the~%~
285                     name ~S. This can't work because the bootstrap~%~
286                     object system doesn't know how to compute effective~%~
287                     slots."
288                    name1)))))
289     slots))
290
291 (defun early-collect-cpl (class-name)
292   (labels ((walk (c)
293              (let* ((definition (early-class-definition c))
294                     (supers (ecd-superclass-names definition)))
295                (cons c
296                      (apply #'append (mapcar #'early-collect-cpl supers))))))
297     (remove-duplicates (walk class-name) :from-end nil :test #'eq)))
298
299 (defun early-collect-default-initargs (cpl)
300   (let ((default-initargs ()))
301     (dolist (class-name cpl)
302       (let* ((definition (early-class-definition class-name))
303              (others (ecd-other-initargs definition)))
304         (loop (when (null others) (return nil))
305               (let ((initarg (pop others)))
306                 (unless (eq initarg :direct-default-initargs)
307                  (error "~@<The defclass option ~S is not supported by ~
308                         the bootstrap object system.~:@>"
309                         initarg)))
310               (setq default-initargs
311                     (nconc default-initargs (reverse (pop others)))))))
312     (reverse default-initargs)))
313
314 (defun !bootstrap-slot-index (class-name slot-name)
315   (or (position slot-name (early-class-slots class-name))
316       (error "~S not found" slot-name)))
317
318 ;;; !BOOTSTRAP-GET-SLOT and !BOOTSTRAP-SET-SLOT are used to access and
319 ;;; change the values of slots during bootstrapping. During
320 ;;; bootstrapping, there are only two kinds of objects whose slots we
321 ;;; need to access, CLASSes and SLOT-DEFINITIONs. The first argument
322 ;;; to these functions tells whether the object is a CLASS or a
323 ;;; SLOT-DEFINITION.
324 ;;;
325 ;;; Note that the way this works it stores the slot in the same place
326 ;;; in memory that the full object system will expect to find it
327 ;;; later. This is critical to the bootstrapping process, the whole
328 ;;; changeover to the full object system is predicated on this.
329 ;;;
330 ;;; One important point is that the layout of standard classes and
331 ;;; standard slots must be computed the same way in this file as it is
332 ;;; by the full object system later.
333 (defmacro !bootstrap-get-slot (type object slot-name)
334   `(clos-slots-ref (get-slots ,object)
335                    (!bootstrap-slot-index ,type ,slot-name)))
336 (defun !bootstrap-set-slot (type object slot-name new-value)
337   (setf (!bootstrap-get-slot type object slot-name) new-value))
338
339 (defun early-class-name (class)
340   (!bootstrap-get-slot 'class class 'name))
341
342 (defun early-class-precedence-list (class)
343   (!bootstrap-get-slot 'pcl-class class 'class-precedence-list))
344
345 (defun early-class-name-of (instance)
346   (early-class-name (class-of instance)))
347
348 (defun early-class-slotds (class)
349   (!bootstrap-get-slot 'slot-class class 'slots))
350
351 (defun early-slot-definition-name (slotd)
352   (!bootstrap-get-slot 'standard-effective-slot-definition slotd 'name))
353
354 (defun early-slot-definition-location (slotd)
355   (!bootstrap-get-slot 'standard-effective-slot-definition slotd 'location))
356
357 (defun early-accessor-method-slot-name (method)
358   (!bootstrap-get-slot 'standard-accessor-method method 'slot-name))
359
360 (unless (fboundp 'class-name-of)
361   (setf (symbol-function 'class-name-of)
362         (symbol-function 'early-class-name-of)))
363 (unintern 'early-class-name-of)
364
365 (defun early-class-direct-subclasses (class)
366   (!bootstrap-get-slot 'class class 'direct-subclasses))
367
368 (declaim (notinline load-defclass))
369 (defun load-defclass (name metaclass supers canonical-slots canonical-options)
370   (setq supers  (copy-tree supers)
371         canonical-slots   (copy-tree canonical-slots)
372         canonical-options (copy-tree canonical-options))
373   (when (eq metaclass 'standard-class)
374     (inform-type-system-about-std-class name))
375   (let ((ecd
376           (make-early-class-definition name
377                                        *load-truename*
378                                        metaclass
379                                        supers
380                                        canonical-slots
381                                        canonical-options))
382         (existing
383           (find name *early-class-definitions* :key #'ecd-class-name)))
384     (setq *early-class-definitions*
385           (cons ecd (remove existing *early-class-definitions*)))
386     ecd))
387