1 ;;;; This software is part of the SBCL system. See the README file for
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
10 ;;;; copyright information from original PCL sources:
12 ;;;; Copyright (c) 1985, 1986, 1987, 1988, 1989, 1990 Xerox Corporation.
13 ;;;; All rights reserved.
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
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
27 (defun make-progn (&rest forms)
28 (let ((progn-form nil))
29 (labels ((collect-forms (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)))))
37 (cons 'progn progn-form))))
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.
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))
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~%~
66 (cl:standard-class 'standard-class)
67 (cl:structure-class 'structure-class)
69 (setf options (remove option options))
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))
81 (mapcar #'(lambda (option)
82 (canonicalize-defclass-option name option))
84 (defstruct-p (and (eq *boot-state* 'complete)
85 (let ((mclass (find-class metaclass nil)))
89 *the-class-structure-class*))))))
93 `(declaim (ftype (function (t) t) ,x)))
96 `(declaim (ftype (function (t t) t) ,x)))
98 (let ,(mapcar #'cdr *initfunctions*)
102 (list ,@canonical-slots)
103 (list ,@(apply #'append
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.
114 ;; (EVAL-WHEN (:COMPILE-TOPLEVEL :LOAD-TOPLEVEL :EXECUTE)
116 ;; (EVAL-WHEN (:LOAD-TOPLEVEL :EXECUTE)
120 (eval defclass-form) ; Define the class now, so that..
121 `(progn ; ..the defstruct can be compiled.
122 ,(class-defstruct-form (find-class name))
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))
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))
157 (let ((entry (assoc initform *initfunctions* :test #'equal)))
159 (setq entry (list initform
161 `(function (lambda () ,initform))))
162 (push entry *initfunctions*))
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))))
172 (error "~S is not a legal slot specification." spec))
174 `'(:name ,(car spec)))
176 (error "In DEFCLASS ~S, the slot specification ~S is obsolete.~%~
178 class-name spec (list (car spec) :initform (cadr spec))))
180 (let* ((name (pop spec))
184 (unsupplied (list nil))
185 (initform (getf spec :initform unsupplied)))
186 (doplist (key val) spec
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
204 (if (eq initform unsupplied)
206 `(list* :initfunction ,(make-initfunction initform) ,@spec))))))
208 (defun canonicalize-defclass-option (class-name option)
209 (declare (ignore class-name))
212 (let ((canonical ()))
213 (let (key val (tail (cdr option)))
214 (loop (when (null tail) (return nil))
217 (push ``(,',key ,,(make-initfunction val) ,',val) canonical))
218 `(':direct-default-initargs (list ,@(nreverse canonical))))))
220 `(',(car option) ',(cadr option)))
222 `(',(car option) ',(cdr option)))))
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.
228 ;;; Each entry in *EARLY-CLASS-DEFINITIONS* is an EARLY-CLASS-DEFINITION.
229 (defparameter *early-class-definitions* ())
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)))
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))
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))
249 (defvar *early-class-slots* nil)
251 (defun canonical-slot-name (canonical-slot)
252 (getf canonical-slot :name))
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*)
262 (defun early-class-size (class-name)
263 (length (early-class-slots class-name)))
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)
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))))))))
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))))
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~%~
291 (defun early-collect-cpl (class-name)
293 (let* ((definition (early-class-definition c))
294 (supers (ecd-superclass-names definition)))
296 (apply #'append (mapcar #'early-collect-cpl supers))))))
297 (remove-duplicates (walk class-name) :from-end nil :test #'eq)))
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.~:@>"
310 (setq default-initargs
311 (nconc default-initargs (reverse (pop others)))))))
312 (reverse default-initargs)))
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)))
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
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.
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))
339 (defun early-class-name (class)
340 (!bootstrap-get-slot 'class class 'name))
342 (defun early-class-precedence-list (class)
343 (!bootstrap-get-slot 'pcl-class class 'class-precedence-list))
345 (defun early-class-name-of (instance)
346 (early-class-name (class-of instance)))
348 (defun early-class-slotds (class)
349 (!bootstrap-get-slot 'slot-class class 'slots))
351 (defun early-slot-definition-name (slotd)
352 (!bootstrap-get-slot 'standard-effective-slot-definition slotd 'name))
354 (defun early-slot-definition-location (slotd)
355 (!bootstrap-get-slot 'standard-effective-slot-definition slotd 'location))
357 (defun early-accessor-method-slot-name (method)
358 (!bootstrap-get-slot 'standard-accessor-method method 'slot-name))
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)
365 (defun early-class-direct-subclasses (class)
366 (!bootstrap-get-slot 'class class 'direct-subclasses))
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))
376 (make-early-class-definition name
383 (find name *early-class-definitions* :key #'ecd-class-name)))
384 (setq *early-class-definitions*
385 (cons ecd (remove existing *early-class-definitions*)))