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