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