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
26 (defun make-progn (&rest forms)
27 (let ((progn-form nil))
28 (labels ((collect-forms (forms)
30 (collect-forms (cdr forms))
31 (if (and (listp (car forms))
32 (eq (caar forms) 'progn))
33 (collect-forms (cdar forms))
34 (push (car forms) progn-form)))))
36 (cons 'progn progn-form))))
38 ;;; Like the DEFMETHOD macro, the expansion of the DEFCLASS macro is
39 ;;; fixed. DEFCLASS always expands into a call to LOAD-DEFCLASS. Until
40 ;;; the meta-braid is set up, LOAD-DEFCLASS has a special definition
41 ;;; which simply collects all class definitions up, when the metabraid
42 ;;; is initialized it is done from those class definitions.
44 ;;; After the metabraid has been setup, and the protocol for defining
45 ;;; classes has been defined, the real definition of LOAD-DEFCLASS is
46 ;;; installed by the file std-class.lisp
47 (defmacro defclass (name %direct-superclasses %direct-slots &rest %options)
48 (let ((supers (copy-tree %direct-superclasses))
49 (slots (copy-tree %direct-slots))
50 (options (copy-tree %options)))
51 (let ((metaclass 'standard-class))
52 (dolist (option options)
53 (if (not (listp option))
54 (error "~S is not a legal defclass option." option)
55 (when (eq (car option) :metaclass)
56 (unless (legal-class-name-p (cadr option))
57 (error "The value of the :metaclass option (~S) is not a~%~
62 (cl:standard-class 'standard-class)
63 (cl:structure-class 'structure-class)
65 (setf options (remove option options))
68 (let ((*initfunctions* ())
69 (*readers* ()) ;Truly a crock, but we got
70 (*writers* ())) ;to have it to live nicely.
71 (declare (special *initfunctions* *readers* *writers*))
72 (let ((canonical-slots
73 (mapcar (lambda (spec)
74 (canonicalize-slot-specification name spec))
77 (mapcar (lambda (option)
78 (canonicalize-defclass-option name option))
80 ;; DEFSTRUCT-P should be true, if the class is defined with a
81 ;; metaclass STRUCTURE-CLASS, such that a DEFSTRUCT is compiled
83 (defstruct-p (and (eq *boot-state* 'complete)
84 (let ((mclass (find-class metaclass nil)))
88 *the-class-structure-class*))))))
92 `(declaim (ftype (function (t) t) ,x)))
95 `(declaim (ftype (function (t t) t) ,x)))
97 (let ,(mapcar #'cdr *initfunctions*)
101 (list ,@canonical-slots)
102 (list ,@(apply #'append
104 '(:from-defclass-p t))
105 other-initargs)))))))
107 (let* ((include (or (and supers
108 (fix-super (car supers)))
109 (and (not (eq name 'structure-object))
110 *the-class-structure-object*)))
111 (defstruct-form (make-structure-class-defstruct-form
112 name slots include)))
114 (eval-when (:compile-toplevel :load-toplevel :execute)
115 ,defstruct-form) ; really compile the defstruct-form
116 (eval-when (:compile-toplevel :load-toplevel :execute)
119 ;; By telling the type system at compile time about
120 ;; the existence of a class named NAME, we can avoid
121 ;; various bogus warnings about "type isn't defined yet"
122 ;; for code elsewhere in the same file which uses
123 ;; the name of the type.
125 ;; But it's not so important to get rid of
126 ;; "not defined yet" warnings during
127 ;; bootstrapping, and machinery like
128 ;; INFORM-TYPE-SYSTEM-ABOUT-STD-CLASS
129 ;; mightn't be defined yet. So punt then.
130 (eq *boot-state* 'complete)
131 ;; And although we know enough about
132 ;; STANDARD-CLASS, and ANSI imposes enough
133 ;; restrictions on the user overloading its
134 ;; methods, that (1) we can shortcut the
135 ;; method dispatch and do an ordinary
136 ;; function call, and (2) be sure we're getting
137 ;; it right even when we do it at compile
138 ;; time; we don't in general know how to do
139 ;; that for other classes. So punt then too.
140 (eq metaclass 'standard-class))
141 `(eval-when (:compile-toplevel)
142 ;; we only need :COMPILE-TOPLEVEL here, because this
143 ;; should happen in the compile-time environment
145 ;; Later, INFORM-TYPE-SYSTEM-ABOUT-STD-CLASS is
146 ;; called by way of LOAD-DEFCLASS (calling
147 ;; ENSURE-CLASS-USING-CLASS) to establish the 'real'
149 (inform-type-system-about-std-class ',name)))
150 ,defclass-form))))))))
152 (defun make-initfunction (initform)
153 (declare (special *initfunctions*))
154 (cond ((or (eq initform t)
155 (equal initform ''t))
156 '(function constantly-t))
157 ((or (eq initform nil)
158 (equal initform ''nil))
159 '(function constantly-nil))
160 ((or (eql initform 0)
161 (equal initform ''0))
162 '(function constantly-0))
164 (let ((entry (assoc initform *initfunctions* :test #'equal)))
166 (setq entry (list initform
168 `(function (lambda () ,initform))))
169 (push entry *initfunctions*))
172 (defun canonicalize-slot-specification (class-name spec)
173 (declare (special *readers* *writers*))
174 (cond ((and (symbolp spec)
175 (not (keywordp spec))
176 (not (memq spec '(t nil))))
179 (error "~S is not a legal slot specification." spec))
181 `'(:name ,(car spec)))
183 (error "In DEFCLASS ~S, the slot specification ~S is obsolete.~%~
185 class-name spec (list (car spec) :initform (cadr spec))))
187 (let* ((name (pop spec))
191 (unsupplied (list nil))
192 (initform (getf spec :initform unsupplied)))
193 (doplist (key val) spec
195 (:accessor (push val readers)
196 (push `(setf ,val) writers))
197 (:reader (push val readers))
198 (:writer (push val writers))
199 (:initarg (push val initargs))))
200 (loop (unless (remf spec :accessor) (return)))
201 (loop (unless (remf spec :reader) (return)))
202 (loop (unless (remf spec :writer) (return)))
203 (loop (unless (remf spec :initarg) (return)))
204 (setq *writers* (append writers *writers*))
205 (setq *readers* (append readers *readers*))
206 (setq spec `(:name ',name
211 (if (eq initform unsupplied)
213 `(list* :initfunction ,(make-initfunction initform) ,@spec))))))
215 (defun canonicalize-defclass-option (class-name option)
216 (declare (ignore class-name))
219 (let ((canonical ()))
220 (let (key val (tail (cdr option)))
221 (loop (when (null tail) (return nil))
224 (push ``(,',key ,,(make-initfunction val) ,',val) canonical))
225 `(:direct-default-initargs (list ,@(nreverse canonical))))))
227 `(',(car option) ',(cadr option)))
229 `(',(car option) ',(cdr option)))))
231 ;;; This is the early definition of load-defclass. It just collects up
232 ;;; all the class definitions in a list. Later, in the file
233 ;;; braid1.lisp, these are actually defined.
235 ;;; Each entry in *EARLY-CLASS-DEFINITIONS* is an EARLY-CLASS-DEFINITION.
236 (defparameter *early-class-definitions* ())
238 (defun early-class-definition (class-name)
239 (or (find class-name *early-class-definitions* :key #'ecd-class-name)
240 (error "~S is not a class in *early-class-definitions*." class-name)))
242 (defun make-early-class-definition
243 (name source metaclass
244 superclass-names canonical-slots other-initargs)
245 (list 'early-class-definition
246 name source metaclass
247 superclass-names canonical-slots other-initargs))
249 (defun ecd-class-name (ecd) (nth 1 ecd))
250 (defun ecd-source (ecd) (nth 2 ecd))
251 (defun ecd-metaclass (ecd) (nth 3 ecd))
252 (defun ecd-superclass-names (ecd) (nth 4 ecd))
253 (defun ecd-canonical-slots (ecd) (nth 5 ecd))
254 (defun ecd-other-initargs (ecd) (nth 6 ecd))
256 (defvar *early-class-slots* nil)
258 (defun canonical-slot-name (canonical-slot)
259 (getf canonical-slot :name))
261 (defun early-class-slots (class-name)
262 (cdr (or (assoc class-name *early-class-slots*)
263 (let ((a (cons class-name
264 (mapcar #'canonical-slot-name
265 (early-collect-inheritance class-name)))))
266 (push a *early-class-slots*)
269 (defun early-class-size (class-name)
270 (length (early-class-slots class-name)))
272 (defun early-collect-inheritance (class-name)
273 ;;(declare (values slots cpl default-initargs direct-subclasses))
274 (let ((cpl (early-collect-cpl class-name)))
275 (values (early-collect-slots cpl)
277 (early-collect-default-initargs cpl)
279 (dolist (definition *early-class-definitions*)
280 (when (memq class-name (ecd-superclass-names definition))
281 (push (ecd-class-name definition) collect)))
282 (nreverse collect)))))
284 (defun early-collect-slots (cpl)
285 (let* ((definitions (mapcar #'early-class-definition cpl))
286 (super-slots (mapcar #'ecd-canonical-slots definitions))
287 (slots (apply #'append (reverse super-slots))))
289 (let ((name1 (canonical-slot-name s1)))
290 (dolist (s2 (cdr (memq s1 slots)))
291 (when (eq name1 (canonical-slot-name s2))
292 (error "More than one early class defines a slot with the~%~
293 name ~S. This can't work because the bootstrap~%~
294 object system doesn't know how to compute effective~%~
299 (defun early-collect-cpl (class-name)
301 (let* ((definition (early-class-definition c))
302 (supers (ecd-superclass-names definition)))
304 (apply #'append (mapcar #'early-collect-cpl supers))))))
305 (remove-duplicates (walk class-name) :from-end nil :test #'eq)))
307 (defun early-collect-default-initargs (cpl)
308 (let ((default-initargs ()))
309 (dolist (class-name cpl)
310 (let* ((definition (early-class-definition class-name))
311 (others (ecd-other-initargs definition)))
312 (loop (when (null others) (return nil))
313 (let ((initarg (pop others)))
314 (unless (eq initarg :direct-default-initargs)
315 (error "~@<The defclass option ~S is not supported by ~
316 the bootstrap object system.~:@>"
318 (setq default-initargs
319 (nconc default-initargs (reverse (pop others)))))))
320 (reverse default-initargs)))
322 (defun !bootstrap-slot-index (class-name slot-name)
323 (or (position slot-name (early-class-slots class-name))
324 (error "~S not found" slot-name)))
326 ;;; !BOOTSTRAP-GET-SLOT and !BOOTSTRAP-SET-SLOT are used to access and
327 ;;; change the values of slots during bootstrapping. During
328 ;;; bootstrapping, there are only two kinds of objects whose slots we
329 ;;; need to access, CLASSes and SLOT-DEFINITIONs. The first argument
330 ;;; to these functions tells whether the object is a CLASS or a
333 ;;; Note that the way this works it stores the slot in the same place
334 ;;; in memory that the full object system will expect to find it
335 ;;; later. This is critical to the bootstrapping process, the whole
336 ;;; changeover to the full object system is predicated on this.
338 ;;; One important point is that the layout of standard classes and
339 ;;; standard slots must be computed the same way in this file as it is
340 ;;; by the full object system later.
341 (defmacro !bootstrap-get-slot (type object slot-name)
342 `(clos-slots-ref (get-slots ,object)
343 (!bootstrap-slot-index ,type ,slot-name)))
344 (defun !bootstrap-set-slot (type object slot-name new-value)
345 (setf (!bootstrap-get-slot type object slot-name) new-value))
347 (defun early-class-name (class)
348 (!bootstrap-get-slot 'class class 'name))
350 (defun early-class-precedence-list (class)
351 (!bootstrap-get-slot 'pcl-class class 'class-precedence-list))
353 (defun early-class-name-of (instance)
354 (early-class-name (class-of instance)))
356 (defun early-class-slotds (class)
357 (!bootstrap-get-slot 'slot-class class 'slots))
359 (defun early-slot-definition-name (slotd)
360 (!bootstrap-get-slot 'standard-effective-slot-definition slotd 'name))
362 (defun early-slot-definition-location (slotd)
363 (!bootstrap-get-slot 'standard-effective-slot-definition slotd 'location))
365 (defun early-accessor-method-slot-name (method)
366 (!bootstrap-get-slot 'standard-accessor-method method 'slot-name))
368 (unless (fboundp 'class-name-of)
369 (setf (symbol-function 'class-name-of)
370 (symbol-function 'early-class-name-of)))
371 (unintern 'early-class-name-of)
373 (defun early-class-direct-subclasses (class)
374 (!bootstrap-get-slot 'class class 'direct-subclasses))
376 (declaim (notinline load-defclass))
377 (defun load-defclass (name metaclass supers canonical-slots canonical-options)
378 (setq supers (copy-tree supers)
379 canonical-slots (copy-tree canonical-slots)
380 canonical-options (copy-tree canonical-options))
381 (when (eq metaclass 'standard-class)
382 (inform-type-system-about-std-class name))
384 (make-early-class-definition name
391 (find name *early-class-definitions* :key #'ecd-class-name)))
392 (setq *early-class-definitions*
393 (cons ecd (remove existing *early-class-definitions*)))