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 std-class.lisp
48 (defmacro defclass (name %direct-superclasses %direct-slots &rest %options)
49 (let ((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~%~
63 (cl:standard-class 'standard-class)
64 (cl:structure-class 'structure-class)
66 (setf options (remove option options))
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))
78 (mapcar #'(lambda (option)
79 (canonicalize-defclass-option name option))
81 ;; DEFSTRUCT-P should be true, if the class is defined with a
82 ;; metaclass STRUCTURE-CLASS, such that a DEFSTRUCT is compiled
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)))))))
108 (let* ((include (or (and supers
109 (fix-super (car supers)))
110 (and (not (eq name 'structure-object))
111 *the-class-structure-object*)))
112 (defstruct-form (make-structure-class-defstruct-form
113 name slots include)))
115 (eval-when (:compile-toplevel :load-toplevel :execute)
116 ,defstruct-form) ; really compile the defstruct-form
117 (eval-when (:compile-toplevel :load-toplevel :execute)
120 ;; By telling the type system at compile time about
121 ;; the existence of a class named NAME, we can avoid
122 ;; various bogus warnings about "type isn't defined yet".
124 ;; But it's not so important to get rid of
125 ;; "not defined yet" warnings during
126 ;; bootstrapping, and machinery like
127 ;; INFORM-TYPE-SYSTEM-ABOUT-STD-CLASS
128 ;; mightn't be defined yet. So punt then.
129 (eq *boot-state* 'complete)
130 ;; And although we know enough about
131 ;; STANDARD-CLASS, and ANSI imposes enough
132 ;; restrictions on the user overloading its
133 ;; methods, that (1) we can shortcut the
134 ;; method dispatch and do an ordinary
135 ;; function call, and (2) be sure we're getting
136 ;; it right even when we do it at compile
137 ;; time; we don't in general know how to do
138 ;; that for other classes. So punt then too.
139 (eq metaclass 'standard-class))
140 `(eval-when (:compile-toplevel)
141 ;; we only need :COMPILE-TOPLEVEL here, because this
142 ;; should happen in the compile-time environment
144 ;; Later, INFORM-TYPE-SYSTEM-ABOUT-STD-CLASS is
145 ;; called by way of LOAD-DEFCLASS (calling
146 ;; ENSURE-CLASS-USING-CLASS) to establish the 'real'
148 (inform-type-system-about-std-class ',name)))
149 ,defclass-form))))))))
151 (defun make-initfunction (initform)
152 (declare (special *initfunctions*))
153 (cond ((or (eq initform t)
154 (equal initform ''t))
155 '(function constantly-t))
156 ((or (eq initform nil)
157 (equal initform ''nil))
158 '(function constantly-nil))
159 ((or (eql initform 0)
160 (equal initform ''0))
161 '(function constantly-0))
163 (let ((entry (assoc initform *initfunctions* :test #'equal)))
165 (setq entry (list initform
167 `(function (lambda () ,initform))))
168 (push entry *initfunctions*))
171 (defun canonicalize-slot-specification (class-name spec)
172 (declare (special *readers* *writers*))
173 (cond ((and (symbolp spec)
174 (not (keywordp spec))
175 (not (memq spec '(t nil))))
178 (error "~S is not a legal slot specification." spec))
180 `'(:name ,(car spec)))
182 (error "In DEFCLASS ~S, the slot specification ~S is obsolete.~%~
184 class-name spec (list (car spec) :initform (cadr spec))))
186 (let* ((name (pop spec))
190 (unsupplied (list nil))
191 (initform (getf spec :initform unsupplied)))
192 (doplist (key val) spec
194 (:accessor (push val readers)
195 (push `(setf ,val) writers))
196 (:reader (push val readers))
197 (:writer (push val writers))
198 (:initarg (push val initargs))))
199 (loop (unless (remf spec :accessor) (return)))
200 (loop (unless (remf spec :reader) (return)))
201 (loop (unless (remf spec :writer) (return)))
202 (loop (unless (remf spec :initarg) (return)))
203 (setq *writers* (append writers *writers*))
204 (setq *readers* (append readers *readers*))
205 (setq spec `(:name ',name
210 (if (eq initform unsupplied)
212 `(list* :initfunction ,(make-initfunction initform) ,@spec))))))
214 (defun canonicalize-defclass-option (class-name option)
215 (declare (ignore class-name))
218 (let ((canonical ()))
219 (let (key val (tail (cdr option)))
220 (loop (when (null tail) (return nil))
223 (push ``(,',key ,,(make-initfunction val) ,',val) canonical))
224 `(':direct-default-initargs (list ,@(nreverse canonical))))))
226 `(',(car option) ',(cadr option)))
228 `(',(car option) ',(cdr option)))))
230 ;;; This is the early definition of load-defclass. It just collects up
231 ;;; all the class definitions in a list. Later, in the file
232 ;;; braid1.lisp, these are actually defined.
234 ;;; Each entry in *EARLY-CLASS-DEFINITIONS* is an EARLY-CLASS-DEFINITION.
235 (defparameter *early-class-definitions* ())
237 (defun early-class-definition (class-name)
238 (or (find class-name *early-class-definitions* :key #'ecd-class-name)
239 (error "~S is not a class in *early-class-definitions*." class-name)))
241 (defun make-early-class-definition
242 (name source metaclass
243 superclass-names canonical-slots other-initargs)
244 (list 'early-class-definition
245 name source metaclass
246 superclass-names canonical-slots other-initargs))
248 (defun ecd-class-name (ecd) (nth 1 ecd))
249 (defun ecd-source (ecd) (nth 2 ecd))
250 (defun ecd-metaclass (ecd) (nth 3 ecd))
251 (defun ecd-superclass-names (ecd) (nth 4 ecd))
252 (defun ecd-canonical-slots (ecd) (nth 5 ecd))
253 (defun ecd-other-initargs (ecd) (nth 6 ecd))
255 (defvar *early-class-slots* nil)
257 (defun canonical-slot-name (canonical-slot)
258 (getf canonical-slot :name))
260 (defun early-class-slots (class-name)
261 (cdr (or (assoc class-name *early-class-slots*)
262 (let ((a (cons class-name
263 (mapcar #'canonical-slot-name
264 (early-collect-inheritance class-name)))))
265 (push a *early-class-slots*)
268 (defun early-class-size (class-name)
269 (length (early-class-slots class-name)))
271 (defun early-collect-inheritance (class-name)
272 ;;(declare (values slots cpl default-initargs direct-subclasses))
273 (let ((cpl (early-collect-cpl class-name)))
274 (values (early-collect-slots cpl)
276 (early-collect-default-initargs cpl)
277 (gathering1 (collecting)
278 (dolist (definition *early-class-definitions*)
279 (when (memq class-name (ecd-superclass-names definition))
280 (gather1 (ecd-class-name definition))))))))
282 (defun early-collect-slots (cpl)
283 (let* ((definitions (mapcar #'early-class-definition cpl))
284 (super-slots (mapcar #'ecd-canonical-slots definitions))
285 (slots (apply #'append (reverse super-slots))))
287 (let ((name1 (canonical-slot-name s1)))
288 (dolist (s2 (cdr (memq s1 slots)))
289 (when (eq name1 (canonical-slot-name s2))
290 (error "More than one early class defines a slot with the~%~
291 name ~S. This can't work because the bootstrap~%~
292 object system doesn't know how to compute effective~%~
297 (defun early-collect-cpl (class-name)
299 (let* ((definition (early-class-definition c))
300 (supers (ecd-superclass-names definition)))
302 (apply #'append (mapcar #'early-collect-cpl supers))))))
303 (remove-duplicates (walk class-name) :from-end nil :test #'eq)))
305 (defun early-collect-default-initargs (cpl)
306 (let ((default-initargs ()))
307 (dolist (class-name cpl)
308 (let* ((definition (early-class-definition class-name))
309 (others (ecd-other-initargs definition)))
310 (loop (when (null others) (return nil))
311 (let ((initarg (pop others)))
312 (unless (eq initarg :direct-default-initargs)
313 (error "~@<The defclass option ~S is not supported by ~
314 the bootstrap object system.~:@>"
316 (setq default-initargs
317 (nconc default-initargs (reverse (pop others)))))))
318 (reverse default-initargs)))
320 (defun !bootstrap-slot-index (class-name slot-name)
321 (or (position slot-name (early-class-slots class-name))
322 (error "~S not found" slot-name)))
324 ;;; !BOOTSTRAP-GET-SLOT and !BOOTSTRAP-SET-SLOT are used to access and
325 ;;; change the values of slots during bootstrapping. During
326 ;;; bootstrapping, there are only two kinds of objects whose slots we
327 ;;; need to access, CLASSes and SLOT-DEFINITIONs. The first argument
328 ;;; to these functions tells whether the object is a CLASS or a
331 ;;; Note that the way this works it stores the slot in the same place
332 ;;; in memory that the full object system will expect to find it
333 ;;; later. This is critical to the bootstrapping process, the whole
334 ;;; changeover to the full object system is predicated on this.
336 ;;; One important point is that the layout of standard classes and
337 ;;; standard slots must be computed the same way in this file as it is
338 ;;; by the full object system later.
339 (defmacro !bootstrap-get-slot (type object slot-name)
340 `(clos-slots-ref (get-slots ,object)
341 (!bootstrap-slot-index ,type ,slot-name)))
342 (defun !bootstrap-set-slot (type object slot-name new-value)
343 (setf (!bootstrap-get-slot type object slot-name) new-value))
345 (defun early-class-name (class)
346 (!bootstrap-get-slot 'class class 'name))
348 (defun early-class-precedence-list (class)
349 (!bootstrap-get-slot 'pcl-class class 'class-precedence-list))
351 (defun early-class-name-of (instance)
352 (early-class-name (class-of instance)))
354 (defun early-class-slotds (class)
355 (!bootstrap-get-slot 'slot-class class 'slots))
357 (defun early-slot-definition-name (slotd)
358 (!bootstrap-get-slot 'standard-effective-slot-definition slotd 'name))
360 (defun early-slot-definition-location (slotd)
361 (!bootstrap-get-slot 'standard-effective-slot-definition slotd 'location))
363 (defun early-accessor-method-slot-name (method)
364 (!bootstrap-get-slot 'standard-accessor-method method 'slot-name))
366 (unless (fboundp 'class-name-of)
367 (setf (symbol-function 'class-name-of)
368 (symbol-function 'early-class-name-of)))
369 (unintern 'early-class-name-of)
371 (defun early-class-direct-subclasses (class)
372 (!bootstrap-get-slot 'class class 'direct-subclasses))
374 (declaim (notinline load-defclass))
375 (defun load-defclass (name metaclass supers canonical-slots canonical-options)
376 (setq supers (copy-tree supers)
377 canonical-slots (copy-tree canonical-slots)
378 canonical-options (copy-tree canonical-options))
379 (when (eq metaclass 'standard-class)
380 (inform-type-system-about-std-class name))
382 (make-early-class-definition name
389 (find name *early-class-definitions* :key #'ecd-class-name)))
390 (setq *early-class-definitions*
391 (cons ecd (remove existing *early-class-definitions*)))