1 ;;;; bootstrapping the meta-braid
3 ;;;; The code in this file takes the early definitions that have been
4 ;;;; saved up and actually builds those class objects. This work is
5 ;;;; largely driven off of those class definitions, but the fact that
6 ;;;; STANDARD-CLASS is the class of all metaclasses in the braid is
7 ;;;; built into this code pretty deeply.
9 ;;;; This software is part of the SBCL system. See the README file for
10 ;;;; more information.
12 ;;;; This software is derived from software originally released by Xerox
13 ;;;; Corporation. Copyright and release statements follow. Later modifications
14 ;;;; to the software are in the public domain and are provided with
15 ;;;; absolutely no warranty. See the COPYING and CREDITS files for more
18 ;;;; copyright information from original PCL sources:
20 ;;;; Copyright (c) 1985, 1986, 1987, 1988, 1989, 1990 Xerox Corporation.
21 ;;;; All rights reserved.
23 ;;;; Use and copying of this software and preparation of derivative works based
24 ;;;; upon this software are permitted. Any distribution of this software or
25 ;;;; derivative works must comply with all applicable United States export
28 ;;;; This software is made available AS IS, and Xerox Corporation makes no
29 ;;;; warranty about the software, its performance or its conformity to any
34 (defun allocate-standard-instance (wrapper
35 &optional (slots-init nil slots-init-p))
36 (let ((instance (%%allocate-instance--class))
37 (no-of-slots (wrapper-no-of-instance-slots wrapper)))
38 (setf (std-instance-wrapper instance) wrapper)
39 (setf (std-instance-slots instance)
41 ;; Inline the slots vector allocation and initialization.
42 (let ((slots (make-array no-of-slots :initial-element 0)))
43 (do ((rem-slots slots-init (rest rem-slots))
45 ((>= i no-of-slots)) ;endp rem-slots))
46 (declare (list rem-slots)
48 (setf (aref slots i) (first rem-slots)))
51 (make-array no-of-slots
52 :initial-element +slot-unbound+))))
55 (defmacro allocate-funcallable-instance-slots (wrapper &optional
56 slots-init-p slots-init)
57 `(let ((no-of-slots (wrapper-no-of-instance-slots ,wrapper)))
60 (make-array no-of-slots :initial-contents ,slots-init)
61 (make-array no-of-slots :initial-element +slot-unbound+))
62 `(make-array no-of-slots :initial-element +slot-unbound+))))
64 (defun allocate-funcallable-instance (wrapper &optional
65 (slots-init nil slots-init-p))
66 (let ((fin (allocate-funcallable-instance-1)))
67 (set-funcallable-instance-function
69 #'(sb-kernel:instance-lambda (&rest args)
70 (declare (ignore args))
71 (error "The function of the funcallable-instance ~S has not been set."
73 (setf (fsc-instance-wrapper fin) wrapper
74 (fsc-instance-slots fin) (allocate-funcallable-instance-slots
75 wrapper slots-init-p slots-init))
78 (defun allocate-structure-instance (wrapper &optional
79 (slots-init nil slots-init-p))
80 (let* ((class (wrapper-class wrapper))
81 (constructor (class-defstruct-constructor class)))
83 (let ((instance (funcall constructor))
84 (slots (class-slots class)))
87 (setf (slot-value-using-class class instance slot)
90 (error "can't allocate an instance of class ~S" (class-name class)))))
92 ;;;; BOOTSTRAP-META-BRAID
94 ;;;; This function builds the base metabraid from the early class definitions.
96 ;;;; FIXME: This, like lotso the other stuff in PCL, is not needed in target
97 ;;;; Lisp, only at bootstrap time. Perhaps we should do something kludgy like
98 ;;;; putting a special character (#\$, perhaps) at the beginning of each
99 ;;;; needed-only-at-bootstrap-time symbol and then UNINTERN them all once we're
100 ;;;; done bootstrapping?
102 (defmacro initial-classes-and-wrappers (&rest classes)
104 ,@(mapcar #'(lambda (class)
105 (let ((wr (intern (format nil "~A-WRAPPER" class)
107 `(setf ,wr ,(if (eq class 'standard-generic-function)
110 (early-class-size ',class)
112 ,class (allocate-standard-instance
113 ,(if (eq class 'standard-generic-function)
114 'funcallable-standard-class-wrapper
115 'standard-class-wrapper))
116 (wrapper-class ,wr) ,class
117 (find-class ',class) ,class)))
120 (defun !bootstrap-meta-braid ()
121 (let* ((*create-classes-from-internal-structure-definitions-p* nil)
122 std-class-wrapper std-class
123 standard-class-wrapper standard-class
124 funcallable-standard-class-wrapper funcallable-standard-class
125 slot-class-wrapper slot-class
126 built-in-class-wrapper built-in-class
127 structure-class-wrapper structure-class
128 standard-direct-slot-definition-wrapper
129 standard-direct-slot-definition
130 standard-effective-slot-definition-wrapper
131 standard-effective-slot-definition
132 class-eq-specializer-wrapper class-eq-specializer
133 standard-generic-function-wrapper standard-generic-function)
134 (initial-classes-and-wrappers
135 standard-class funcallable-standard-class
136 slot-class built-in-class structure-class std-class
137 standard-direct-slot-definition standard-effective-slot-definition
138 class-eq-specializer standard-generic-function)
139 ;; First, make a class metaobject for each of the early classes. For
140 ;; each metaobject we also set its wrapper. Except for the class T,
141 ;; the wrapper is always that of STANDARD-CLASS.
142 (dolist (definition *early-class-definitions*)
143 (let* ((name (ecd-class-name definition))
144 (meta (ecd-metaclass definition))
146 (slot-class slot-class-wrapper)
147 (std-class std-class-wrapper)
148 (standard-class standard-class-wrapper)
149 (funcallable-standard-class
150 funcallable-standard-class-wrapper)
151 (built-in-class built-in-class-wrapper)
152 (structure-class structure-class-wrapper)))
153 (class (or (find-class name nil)
154 (allocate-standard-instance wrapper))))
155 (when (or (eq meta 'standard-class)
156 (eq meta 'funcallable-standard-class))
157 (inform-type-system-about-std-class name))
158 (setf (find-class name) class)))
159 (dolist (definition *early-class-definitions*)
160 (let ((name (ecd-class-name definition))
161 (meta (ecd-metaclass definition))
162 (source (ecd-source definition))
163 (direct-supers (ecd-superclass-names definition))
164 (direct-slots (ecd-canonical-slots definition))
165 (other-initargs (ecd-other-initargs definition)))
166 (let ((direct-default-initargs
167 (getf other-initargs :direct-default-initargs)))
168 (multiple-value-bind (slots cpl default-initargs direct-subclasses)
169 (early-collect-inheritance name)
170 (let* ((class (find-class name))
171 (wrapper (cond ((eq class slot-class)
173 ((eq class std-class)
175 ((eq class standard-class)
176 standard-class-wrapper)
177 ((eq class funcallable-standard-class)
178 funcallable-standard-class-wrapper)
179 ((eq class standard-direct-slot-definition)
180 standard-direct-slot-definition-wrapper)
182 standard-effective-slot-definition)
183 standard-effective-slot-definition-wrapper)
184 ((eq class built-in-class)
185 built-in-class-wrapper)
186 ((eq class structure-class)
187 structure-class-wrapper)
188 ((eq class class-eq-specializer)
189 class-eq-specializer-wrapper)
190 ((eq class standard-generic-function)
191 standard-generic-function-wrapper)
193 (boot-make-wrapper (length slots) name))))
195 (when (eq name t) (setq *the-wrapper-of-t* wrapper))
196 (set (intern (format nil "*THE-CLASS-~A*" (symbol-name name))
200 (unless (eq (getf slot :allocation :instance) :instance)
201 (error "Slot allocation ~S is not supported in bootstrap.")))
203 (when (typep wrapper 'wrapper)
204 (setf (wrapper-instance-slots-layout wrapper)
205 (mapcar #'canonical-slot-name slots))
206 (setf (wrapper-class-slots wrapper)
209 (setq proto (if (eq meta 'funcallable-standard-class)
210 (allocate-funcallable-instance wrapper)
211 (allocate-standard-instance wrapper)))
214 (!bootstrap-make-slot-definitions
215 name class direct-slots
216 standard-direct-slot-definition-wrapper nil))
218 (!bootstrap-make-slot-definitions
220 standard-effective-slot-definition-wrapper t))
223 ((std-class standard-class funcallable-standard-class)
224 (!bootstrap-initialize-class
226 class name class-eq-specializer-wrapper source
227 direct-supers direct-subclasses cpl wrapper proto
228 direct-slots slots direct-default-initargs default-initargs))
229 (built-in-class ; *the-class-t*
230 (!bootstrap-initialize-class
232 class name class-eq-specializer-wrapper source
233 direct-supers direct-subclasses cpl wrapper proto))
234 (slot-class ; *the-class-slot-object*
235 (!bootstrap-initialize-class
237 class name class-eq-specializer-wrapper source
238 direct-supers direct-subclasses cpl wrapper proto))
239 (structure-class ; *the-class-structure-object*
240 (!bootstrap-initialize-class
242 class name class-eq-specializer-wrapper source
243 direct-supers direct-subclasses cpl wrapper))))))))
245 (let* ((smc-class (find-class 'standard-method-combination))
246 (smc-wrapper (!bootstrap-get-slot 'standard-class
249 (smc (allocate-standard-instance smc-wrapper)))
250 (flet ((set-slot (name value)
251 (!bootstrap-set-slot 'standard-method-combination
255 (set-slot 'source *load-truename*)
256 (set-slot 'type 'standard)
257 (set-slot 'documentation "The standard method combination.")
258 (set-slot 'options ()))
259 (setq *standard-method-combination* smc))))
261 ;;; Initialize a class metaobject.
262 (defun !bootstrap-initialize-class
263 (metaclass-name class name
264 class-eq-wrapper source direct-supers direct-subclasses cpl wrapper
266 proto direct-slots slots direct-default-initargs default-initargs)
267 (flet ((classes (names) (mapcar #'find-class names))
268 (set-slot (slot-name value)
269 (!bootstrap-set-slot metaclass-name class slot-name value)))
270 (set-slot 'name name)
271 (set-slot 'source source)
272 (set-slot 'type (if (eq class (find-class t))
274 ;; FIXME: Could this just be CLASS instead
275 ;; of `(CLASS ,CLASS)? If not, why not?
276 ;; (See also similar expression in
277 ;; SHARED-INITIALIZE :BEFORE (CLASS).)
279 (set-slot 'class-eq-specializer
280 (let ((spec (allocate-standard-instance class-eq-wrapper)))
281 (!bootstrap-set-slot 'class-eq-specializer spec 'type
283 (!bootstrap-set-slot 'class-eq-specializer spec 'object
286 (set-slot 'class-precedence-list (classes cpl))
287 (set-slot 'can-precede-list (classes (cdr cpl)))
288 (set-slot 'incompatible-superclass-list nil)
289 (set-slot 'direct-superclasses (classes direct-supers))
290 (set-slot 'direct-subclasses (classes direct-subclasses))
291 (set-slot 'direct-methods (cons nil nil))
292 (set-slot 'wrapper wrapper)
293 (set-slot 'predicate-name (or (cadr (assoc name *early-class-predicates*))
294 (make-class-predicate-name name)))
296 `(,@(and direct-default-initargs
297 `(direct-default-initargs ,direct-default-initargs))
298 ,@(and default-initargs
299 `(default-initargs ,default-initargs))))
300 (when (memq metaclass-name '(standard-class funcallable-standard-class
301 structure-class slot-class std-class))
302 (set-slot 'direct-slots direct-slots)
303 (set-slot 'slots slots)
304 (set-slot 'initialize-info nil))
305 (if (eq metaclass-name 'structure-class)
306 (let ((constructor-sym '|STRUCTURE-OBJECT class constructor|))
307 (set-slot 'predicate-name (or (cadr (assoc name
308 *early-class-predicates*))
309 (make-class-predicate-name name)))
310 (set-slot 'defstruct-form
311 `(defstruct (structure-object (:constructor
314 (set-slot 'defstruct-constructor constructor-sym)
315 (set-slot 'from-defclass-p t)
316 (set-slot 'plist nil)
317 (set-slot 'prototype (funcall constructor-sym)))
318 (set-slot 'prototype (or proto (allocate-standard-instance wrapper))))
321 (defun !bootstrap-make-slot-definitions (name class slots wrapper effective-p)
323 (mapcar (lambda (slot)
325 (!bootstrap-make-slot-definition
326 name class slot wrapper effective-p index))
329 (defun !bootstrap-make-slot-definition
330 (name class slot wrapper effective-p index)
331 (let* ((slotd-class-name (if effective-p
332 'standard-effective-slot-definition
333 'standard-direct-slot-definition))
334 (slotd (allocate-standard-instance wrapper))
335 (slot-name (getf slot :name)))
336 (flet ((get-val (name) (getf slot name))
338 (!bootstrap-set-slot slotd-class-name slotd name val)))
339 (set-val 'name slot-name)
340 (set-val 'initform (get-val :initform))
341 (set-val 'initfunction (get-val :initfunction))
342 (set-val 'initargs (get-val :initargs))
343 (set-val 'readers (get-val :readers))
344 (set-val 'writers (get-val :writers))
345 (set-val 'allocation :instance)
346 (set-val 'type (or (get-val :type) t))
347 (set-val 'documentation (or (get-val :documentation) ""))
348 (set-val 'class class)
350 (set-val 'location index)
352 (set-val 'reader-function (make-optimized-std-reader-method-function
353 fsc-p slot-name index))
354 (set-val 'writer-function (make-optimized-std-writer-method-function
355 fsc-p slot-name index))
356 (set-val 'boundp-function (make-optimized-std-boundp-method-function
357 fsc-p slot-name index)))
358 (set-val 'accessor-flags 7)
359 (let ((table (or (gethash slot-name *name->class->slotd-table*)
360 (setf (gethash slot-name *name->class->slotd-table*)
361 (make-hash-table :test 'eq :size 5)))))
362 (setf (gethash class table) slotd)))
363 (when (and (eq name 'standard-class)
364 (eq slot-name 'slots) effective-p)
365 (setq *the-eslotd-standard-class-slots* slotd))
366 (when (and (eq name 'funcallable-standard-class)
367 (eq slot-name 'slots) effective-p)
368 (setq *the-eslotd-funcallable-standard-class-slots* slotd))
371 (defun !bootstrap-accessor-definitions (early-p)
372 (let ((*early-p* early-p))
373 (dolist (definition *early-class-definitions*)
374 (let ((name (ecd-class-name definition))
375 (meta (ecd-metaclass definition)))
376 (unless (eq meta 'built-in-class)
377 (let ((direct-slots (ecd-canonical-slots definition)))
378 (dolist (slotd direct-slots)
379 (let ((slot-name (getf slotd :name))
380 (readers (getf slotd :readers))
381 (writers (getf slotd :writers)))
382 (!bootstrap-accessor-definitions1
388 (!bootstrap-accessor-definitions1
391 (list (slot-reader-symbol slot-name))
392 (list (slot-writer-symbol slot-name))
393 (list (slot-boundp-symbol slot-name)))))))))))
395 (defun !bootstrap-accessor-definition (class-name accessor-name slot-name type)
396 (multiple-value-bind (accessor-class make-method-function arglist specls doc)
398 (reader (values 'standard-reader-method
399 #'make-std-reader-method-function
402 "automatically generated reader method"))
403 (writer (values 'standard-writer-method
404 #'make-std-writer-method-function
405 (list 'new-value class-name)
407 "automatically generated writer method"))
408 (boundp (values 'standard-boundp-method
409 #'make-std-boundp-method-function
412 "automatically generated boundp method")))
413 (let ((gf (ensure-generic-function accessor-name)))
414 (if (find specls (early-gf-methods gf)
415 :key #'early-method-specializers
417 (unless (assoc accessor-name *!generic-function-fixups*
421 (make-a-method accessor-class
424 (funcall make-method-function
425 class-name slot-name)
429 (defun !bootstrap-accessor-definitions1 (class-name
434 (flet ((do-reader-definition (reader)
435 (!bootstrap-accessor-definition class-name
439 (do-writer-definition (writer)
440 (!bootstrap-accessor-definition class-name
444 (do-boundp-definition (boundp)
445 (!bootstrap-accessor-definition class-name
449 (dolist (reader readers) (do-reader-definition reader))
450 (dolist (writer writers) (do-writer-definition writer))
451 (dolist (boundp boundps) (do-boundp-definition boundp))))
453 (defun !bootstrap-class-predicates (early-p)
454 (let ((*early-p* early-p))
455 (dolist (definition *early-class-definitions*)
456 (let* ((name (ecd-class-name definition))
457 (class (find-class name)))
458 (setf (find-class-predicate name)
459 (make-class-predicate class (class-predicate-name class)))))))
461 (defun !bootstrap-built-in-classes ()
463 ;; First make sure that all the supers listed in
464 ;; *BUILT-IN-CLASS-LATTICE* are themselves defined by
465 ;; *BUILT-IN-CLASS-LATTICE*. This is just to check for typos and
466 ;; other sorts of brainos.
467 (dolist (e *built-in-classes*)
468 (dolist (super (cadr e))
469 (unless (or (eq super t)
470 (assq super *built-in-classes*))
471 (error "in *BUILT-IN-CLASSES*: ~S has ~S as a super,~%~
472 but ~S is not itself a class in *BUILT-IN-CLASSES*."
473 (car e) super super))))
475 ;; In the first pass, we create a skeletal object to be bound to the
477 (let* ((built-in-class (find-class 'built-in-class))
478 (built-in-class-wrapper (class-wrapper built-in-class)))
479 (dolist (e *built-in-classes*)
480 (let ((class (allocate-standard-instance built-in-class-wrapper)))
481 (setf (find-class (car e)) class))))
483 ;; In the second pass, we initialize the class objects.
484 (let ((class-eq-wrapper (class-wrapper (find-class 'class-eq-specializer))))
485 (dolist (e *built-in-classes*)
486 (destructuring-bind (name supers subs cpl prototype) e
487 (let* ((class (find-class name))
488 (lclass (cl:find-class name))
489 (wrapper (sb-kernel:class-layout lclass)))
490 (set (get-built-in-class-symbol name) class)
491 (set (get-built-in-wrapper-symbol name) wrapper)
492 (setf (sb-kernel:class-pcl-class lclass) class)
494 (!bootstrap-initialize-class 'built-in-class class
495 name class-eq-wrapper nil
498 wrapper prototype)))))
500 (dolist (e *built-in-classes*)
501 (let* ((name (car e))
502 (class (find-class name)))
503 (setf (find-class-predicate name)
504 (make-class-predicate class (class-predicate-name class))))))
506 (defmacro wrapper-of-macro (x)
507 `(sb-kernel:layout-of ,x))
510 (wrapper-class* (wrapper-of-macro x)))
512 ;;; FIXME: We probably don't need both WRAPPER-OF and WRAPPER-OF-MACRO.
513 #-sb-fluid (declaim (inline wrapper-of))
514 (defun wrapper-of (x)
515 (wrapper-of-macro x))
517 (defvar *find-structure-class* nil)
519 (defun eval-form (form)
520 #'(lambda () (eval form)))
522 (defun slot-initargs-from-structure-slotd (slotd)
523 `(:name ,(structure-slotd-name slotd)
524 :defstruct-accessor-symbol ,(structure-slotd-accessor-symbol slotd)
525 :internal-reader-function ,(structure-slotd-reader-function slotd)
526 :internal-writer-function ,(structure-slotd-writer-function slotd)
527 :type ,(or (structure-slotd-type slotd) t)
528 :initform ,(structure-slotd-init-form slotd)
529 :initfunction ,(eval-form (structure-slotd-init-form slotd))))
531 (defun find-structure-class (symbol)
532 (if (structure-type-p symbol)
533 (unless (eq *find-structure-class* symbol)
534 (let ((*find-structure-class* symbol))
536 :metaclass 'structure-class
539 (cond ;; Handle our CMU-CL-ish structure-based
541 ((cl:subtypep symbol 'condition)
542 (mapcar #'cl:class-name
543 (sb-kernel:class-direct-superclasses
544 (cl:find-class symbol))))
545 ;; a hack to add the STREAM class as a
546 ;; mixin to the LISP-STREAM class.
547 ((eq symbol 'sb-kernel:lisp-stream)
548 '(structure-object stream))
549 ((structure-type-included-type-name symbol)
550 (list (structure-type-included-type-name
553 (mapcar #'slot-initargs-from-structure-slotd
554 (structure-type-slot-description-list
556 (error "~S is not a legal structure class name." symbol)))
558 (defun make-class-predicate (class name)
559 (let* ((gf (ensure-generic-function name))
560 (mlist (if (eq *boot-state* 'complete)
561 (generic-function-methods gf)
562 (early-gf-methods gf))))
564 (unless (eq class *the-class-t*)
565 (let* ((default-method-function #'constantly-nil)
566 (default-method-initargs (list :function
567 default-method-function))
568 (default-method (make-a-method 'standard-method
572 default-method-initargs
573 "class predicate default method")))
574 (setf (method-function-get default-method-function :constant-value)
576 (add-method gf default-method)))
577 (let* ((class-method-function #'constantly-t)
578 (class-method-initargs (list :function
579 class-method-function))
580 (class-method (make-a-method 'standard-method
584 class-method-initargs
585 "class predicate class method")))
586 (setf (method-function-get class-method-function :constant-value) t)
587 (add-method gf class-method)))
590 ;;; Set the inherits from CPL, and register the layout. This actually
591 ;;; installs the class in the Lisp type system.
592 (defun update-lisp-class-layout (class layout)
593 (let ((lclass (sb-kernel:layout-class layout)))
594 (unless (eq (sb-kernel:class-layout lclass) layout)
595 (setf (sb-kernel:layout-inherits layout)
596 (map 'vector #'class-wrapper
597 (reverse (rest (class-precedence-list class)))))
598 (sb-kernel:register-layout layout :invalidate nil)
600 ;; Subclasses of formerly forward-referenced-class may be
601 ;; unknown to CL:FIND-CLASS and also anonymous. This
602 ;; functionality moved here from (SETF FIND-CLASS).
603 (let ((name (class-name class)))
604 (setf (cl:find-class name) lclass
605 ;; FIXME: It's nasty to use double colons. Perhaps the
606 ;; best way to fix this is not to export CLASS-%NAME
607 ;; from SB-KERNEL, but instead to move the whole
608 ;; UPDATE-LISP-CLASS-LAYOUT function to SB-KERNEL, and
609 ;; export it. (since it's also nasty for us to be
610 ;; reaching into %KERNEL implementation details my
611 ;; messing with raw CLASS-%NAME)
612 (sb-kernel::class-%name lclass) name)))))
614 (clrhash *find-class*)
615 (!bootstrap-meta-braid)
616 (!bootstrap-accessor-definitions t)
617 (!bootstrap-class-predicates t)
618 (!bootstrap-accessor-definitions nil)
619 (!bootstrap-class-predicates nil)
620 (!bootstrap-built-in-classes)
622 (dohash (name x *find-class*)
623 (let* ((class (find-class-from-cell name x))
624 (layout (class-wrapper class))
625 (lclass (sb-kernel:layout-class layout))
626 (lclass-pcl-class (sb-kernel:class-pcl-class lclass))
627 (olclass (cl:find-class name nil)))
629 (aver (eq class lclass-pcl-class))
630 (setf (sb-kernel:class-pcl-class lclass) class))
632 (update-lisp-class-layout class layout)
635 (aver (eq lclass olclass)))
637 (setf (cl:find-class name) lclass)))))
639 (setq *boot-state* 'braid)
641 (defmethod no-applicable-method (generic-function &rest args)
642 (error "~@<There is no matching method for the generic function ~2I~_~S~
643 ~I~_when called with arguments ~2I~_~S.~:>"