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 ;; 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~%~
70 (cl:standard-class 'standard-class)
71 (cl:structure-class 'structure-class)
73 (setf options (remove option options))
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))
85 (mapcar #'(lambda (option)
86 (canonicalize-defclass-option name option))
88 (defstruct-p (and (eq *boot-state* 'complete)
89 (let ((mclass (find-class metaclass nil)))
93 *the-class-structure-class*))))))
95 (eval-when (:load-toplevel :execute)
97 ,@(mapcar #'(lambda (x)
98 `(declaim (ftype (function (t) t) ,x)))
100 ,@(mapcar #'(lambda (x)
101 `(declaim (ftype (function (t t) t) ,x)))
103 (let ,(mapcar #'cdr *initfunctions*)
104 (load-defclass ',name
107 (list ,@canonical-slots)
108 (list ,@(apply #'append
110 '(:from-defclass-p t))
111 other-initargs))))))))
114 (eval defclass-form) ; Define the class now, so that..
115 `(progn ; ..the defstruct can be compiled.
116 ,(class-defstruct-form (find-class name))
119 (when (eq *boot-state* 'complete)
120 (inform-type-system-about-std-class name))
123 (defun make-initfunction (initform)
124 (declare (special *initfunctions*))
125 (cond ((or (eq initform 't)
126 (equal initform ''t))
127 '(function constantly-t))
128 ((or (eq initform 'nil)
129 (equal initform ''nil))
130 '(function constantly-nil))
131 ((or (eql initform '0)
132 (equal initform ''0))
133 '(function constantly-0))
135 (let ((entry (assoc initform *initfunctions* :test #'equal)))
137 (setq entry (list initform
139 `(function (lambda () ,initform))))
140 (push entry *initfunctions*))
143 (defun canonicalize-slot-specification (class-name spec)
144 (declare (special *readers* *writers*))
145 (cond ((and (symbolp spec)
146 (not (keywordp spec))
147 (not (memq spec '(t nil))))
150 (error "~S is not a legal slot specification." spec))
152 `'(:name ,(car spec)))
154 (error "In DEFCLASS ~S, the slot specification ~S is obsolete.~%~
156 class-name spec (list (car spec) :initform (cadr spec))))
158 (let* ((name (pop spec))
162 (unsupplied (list nil))
163 (initform (getf spec :initform unsupplied)))
164 (doplist (key val) spec
166 (:accessor (push val readers)
167 (push `(setf ,val) writers))
168 (:reader (push val readers))
169 (:writer (push val writers))
170 (:initarg (push val initargs))))
171 (loop (unless (remf spec :accessor) (return)))
172 (loop (unless (remf spec :reader) (return)))
173 (loop (unless (remf spec :writer) (return)))
174 (loop (unless (remf spec :initarg) (return)))
175 (setq *writers* (append writers *writers*))
176 (setq *readers* (append readers *readers*))
177 (setq spec `(:name ',name
182 (if (eq initform unsupplied)
184 `(list* :initfunction ,(make-initfunction initform) ,@spec))))))
186 (defun canonicalize-defclass-option (class-name option)
187 (declare (ignore class-name))
190 (let ((canonical ()))
191 (let (key val (tail (cdr option)))
192 (loop (when (null tail) (return nil))
195 (push ``(,',key ,,(make-initfunction val) ,',val) canonical))
196 `(':direct-default-initargs (list ,@(nreverse canonical))))))
198 `(',(car option) ',(cadr option)))
200 `(',(car option) ',(cdr option)))))
202 ;;; This is the early definition of load-defclass. It just collects up
203 ;;; all the class definitions in a list. Later, in the file
204 ;;; braid1.lisp, these are actually defined.
206 ;;; Each entry in *EARLY-CLASS-DEFINITIONS* is an EARLY-CLASS-DEFINITION.
207 (defparameter *early-class-definitions* ())
209 (defun early-class-definition (class-name)
210 (or (find class-name *early-class-definitions* :key #'ecd-class-name)
211 (error "~S is not a class in *early-class-definitions*." class-name)))
213 (defun make-early-class-definition
214 (name source metaclass
215 superclass-names canonical-slots other-initargs)
216 (list 'early-class-definition
217 name source metaclass
218 superclass-names canonical-slots other-initargs))
220 (defun ecd-class-name (ecd) (nth 1 ecd))
221 (defun ecd-source (ecd) (nth 2 ecd))
222 (defun ecd-metaclass (ecd) (nth 3 ecd))
223 (defun ecd-superclass-names (ecd) (nth 4 ecd))
224 (defun ecd-canonical-slots (ecd) (nth 5 ecd))
225 (defun ecd-other-initargs (ecd) (nth 6 ecd))
227 (defvar *early-class-slots* nil)
229 (defun canonical-slot-name (canonical-slot)
230 (getf canonical-slot :name))
232 (defun early-class-slots (class-name)
233 (cdr (or (assoc class-name *early-class-slots*)
234 (let ((a (cons class-name
235 (mapcar #'canonical-slot-name
236 (early-collect-inheritance class-name)))))
237 (push a *early-class-slots*)
240 (defun early-class-size (class-name)
241 (length (early-class-slots class-name)))
243 (defun early-collect-inheritance (class-name)
244 ;;(declare (values slots cpl default-initargs direct-subclasses))
245 (let ((cpl (early-collect-cpl class-name)))
246 (values (early-collect-slots cpl)
248 (early-collect-default-initargs cpl)
249 (gathering1 (collecting)
250 (dolist (definition *early-class-definitions*)
251 (when (memq class-name (ecd-superclass-names definition))
252 (gather1 (ecd-class-name definition))))))))
254 (defun early-collect-slots (cpl)
255 (let* ((definitions (mapcar #'early-class-definition cpl))
256 (super-slots (mapcar #'ecd-canonical-slots definitions))
257 (slots (apply #'append (reverse super-slots))))
259 (let ((name1 (canonical-slot-name s1)))
260 (dolist (s2 (cdr (memq s1 slots)))
261 (when (eq name1 (canonical-slot-name s2))
262 (error "More than one early class defines a slot with the~%~
263 name ~S. This can't work because the bootstrap~%~
264 object system doesn't know how to compute effective~%~
269 (defun early-collect-cpl (class-name)
271 (let* ((definition (early-class-definition c))
272 (supers (ecd-superclass-names definition)))
274 (apply #'append (mapcar #'early-collect-cpl supers))))))
275 (remove-duplicates (walk class-name) :from-end nil :test #'eq)))
277 (defun early-collect-default-initargs (cpl)
278 (let ((default-initargs ()))
279 (dolist (class-name cpl)
280 (let* ((definition (early-class-definition class-name))
281 (others (ecd-other-initargs definition)))
282 (loop (when (null others) (return nil))
283 (let ((initarg (pop others)))
284 (unless (eq initarg :direct-default-initargs)
285 (error "~@<The defclass option ~S is not supported by ~
286 the bootstrap object system.~:@>"
288 (setq default-initargs
289 (nconc default-initargs (reverse (pop others)))))))
290 (reverse default-initargs)))
292 (defun !bootstrap-slot-index (class-name slot-name)
293 (or (position slot-name (early-class-slots class-name))
294 (error "~S not found" slot-name)))
296 ;;; !BOOTSTRAP-GET-SLOT and !BOOTSTRAP-SET-SLOT are used to access and
297 ;;; change the values of slots during bootstrapping. During
298 ;;; bootstrapping, there are only two kinds of objects whose slots we
299 ;;; need to access, CLASSes and SLOT-DEFINITIONs. The first argument
300 ;;; to these functions tells whether the object is a CLASS or a
303 ;;; Note that the way this works it stores the slot in the same place
304 ;;; in memory that the full object system will expect to find it
305 ;;; later. This is critical to the bootstrapping process, the whole
306 ;;; changeover to the full object system is predicated on this.
308 ;;; One important point is that the layout of standard classes and
309 ;;; standard slots must be computed the same way in this file as it is
310 ;;; by the full object system later.
311 (defmacro !bootstrap-get-slot (type object slot-name)
312 `(clos-slots-ref (get-slots ,object)
313 (!bootstrap-slot-index ,type ,slot-name)))
314 (defun !bootstrap-set-slot (type object slot-name new-value)
315 (setf (!bootstrap-get-slot type object slot-name) new-value))
317 (defun early-class-name (class)
318 (!bootstrap-get-slot 'class class 'name))
320 (defun early-class-precedence-list (class)
321 (!bootstrap-get-slot 'pcl-class class 'class-precedence-list))
323 (defun early-class-name-of (instance)
324 (early-class-name (class-of instance)))
326 (defun early-class-slotds (class)
327 (!bootstrap-get-slot 'slot-class class 'slots))
329 (defun early-slot-definition-name (slotd)
330 (!bootstrap-get-slot 'standard-effective-slot-definition slotd 'name))
332 (defun early-slot-definition-location (slotd)
333 (!bootstrap-get-slot 'standard-effective-slot-definition slotd 'location))
335 (defun early-accessor-method-slot-name (method)
336 (!bootstrap-get-slot 'standard-accessor-method method 'slot-name))
338 (unless (fboundp 'class-name-of)
339 (setf (symbol-function 'class-name-of)
340 (symbol-function 'early-class-name-of)))
341 (unintern 'early-class-name-of)
343 (defun early-class-direct-subclasses (class)
344 (!bootstrap-get-slot 'class class 'direct-subclasses))
346 (declaim (notinline load-defclass))
347 (defun load-defclass (name metaclass supers canonical-slots canonical-options)
348 (setq supers (copy-tree supers)
349 canonical-slots (copy-tree canonical-slots)
350 canonical-options (copy-tree canonical-options))
351 (when (eq metaclass 'standard-class)
352 (inform-type-system-about-std-class name))
354 (make-early-class-definition name
361 (find name *early-class-definitions* :key #'ecd-class-name)))
362 (setq *early-class-definitions*
363 (cons ecd (remove existing *early-class-definitions*)))