0.9.5.58:
[sbcl.git] / src / pcl / braid.lisp
1 ;;;; bootstrapping the meta-braid
2 ;;;;
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.
8
9 ;;;; This software is part of the SBCL system. See the README file for
10 ;;;; more information.
11
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
16 ;;;; information.
17
18 ;;;; copyright information from original PCL sources:
19 ;;;;
20 ;;;; Copyright (c) 1985, 1986, 1987, 1988, 1989, 1990 Xerox Corporation.
21 ;;;; All rights reserved.
22 ;;;;
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
26 ;;;; control laws.
27 ;;;;
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
30 ;;;; specification.
31
32 (in-package "SB-PCL")
33 \f
34 (defun allocate-standard-instance (wrapper
35                                    &optional (slots-init nil slots-init-p))
36   (let ((instance (%make-standard-instance nil (get-instance-hash-code)))
37         (no-of-slots (wrapper-no-of-instance-slots wrapper)))
38     (setf (std-instance-wrapper instance) wrapper)
39     (setf (std-instance-slots instance)
40           (cond (slots-init-p
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))
44                         (i 0 (1+ i)))
45                        ((>= i no-of-slots)) ;endp rem-slots))
46                      (declare (list rem-slots)
47                               (type index i))
48                      (setf (aref slots i) (first rem-slots)))
49                    slots))
50                 (t
51                  (make-array no-of-slots
52                              :initial-element +slot-unbound+))))
53     instance))
54
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)))
58      ,(if slots-init-p
59           `(if ,slots-init-p
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+))))
63
64 (defun allocate-funcallable-instance (wrapper &optional
65                                               (slots-init nil slots-init-p))
66   (let ((fin (%make-pcl-funcallable-instance nil nil
67                                              (get-instance-hash-code))))
68     (set-funcallable-instance-function
69      fin
70      #'(lambda (&rest args)
71          (declare (ignore args))
72          (error "The function of the funcallable-instance ~S has not been set."
73                 fin)))
74     (setf (fsc-instance-wrapper fin) wrapper
75           (fsc-instance-slots fin) (allocate-funcallable-instance-slots
76                                     wrapper slots-init-p slots-init))
77     fin))
78
79 (defun allocate-structure-instance (wrapper &optional
80                                             (slots-init nil slots-init-p))
81   (let* ((class (wrapper-class wrapper))
82          (constructor (class-defstruct-constructor class)))
83     (if constructor
84         (let ((instance (funcall constructor))
85               (slots (class-slots class)))
86           (when slots-init-p
87             (dolist (slot slots)
88               (setf (slot-value-using-class class instance slot)
89                     (pop slots-init))))
90           instance)
91         (error "can't allocate an instance of class ~S" (class-name class)))))
92 \f
93 ;;;; BOOTSTRAP-META-BRAID
94 ;;;;
95 ;;;; This function builds the base metabraid from the early class definitions.
96
97 (defmacro !initial-classes-and-wrappers (&rest classes)
98   `(progn
99      ,@(mapcar (lambda (class)
100                  (let ((wr (format-symbol *pcl-package* "~A-WRAPPER" class)))
101                    `(setf ,wr ,(if (eq class 'standard-generic-function)
102                                    '*sgf-wrapper*
103                                    `(boot-make-wrapper
104                                      (early-class-size ',class)
105                                      ',class))
106                           ,class (allocate-standard-instance
107                                   ,(if (eq class 'standard-generic-function)
108                                        'funcallable-standard-class-wrapper
109                                        'standard-class-wrapper))
110                           (wrapper-class ,wr) ,class
111                           (find-class ',class) ,class)))
112                classes)))
113
114 (defun !bootstrap-meta-braid ()
115   (let* ((*create-classes-from-internal-structure-definitions-p* nil)
116          standard-class-wrapper standard-class
117          funcallable-standard-class-wrapper funcallable-standard-class
118          slot-class-wrapper slot-class
119          built-in-class-wrapper built-in-class
120          structure-class-wrapper structure-class
121          condition-class-wrapper condition-class
122          standard-direct-slot-definition-wrapper
123          standard-direct-slot-definition
124          standard-effective-slot-definition-wrapper
125          standard-effective-slot-definition
126          class-eq-specializer-wrapper class-eq-specializer
127          standard-generic-function-wrapper standard-generic-function)
128     (!initial-classes-and-wrappers
129      standard-class funcallable-standard-class
130      slot-class built-in-class structure-class condition-class
131      standard-direct-slot-definition standard-effective-slot-definition
132      class-eq-specializer standard-generic-function)
133     ;; First, make a class metaobject for each of the early classes. For
134     ;; each metaobject we also set its wrapper. Except for the class T,
135     ;; the wrapper is always that of STANDARD-CLASS.
136     (dolist (definition *early-class-definitions*)
137       (let* ((name (ecd-class-name definition))
138              (meta (ecd-metaclass definition))
139              (wrapper (ecase meta
140                         (slot-class slot-class-wrapper)
141                         (standard-class standard-class-wrapper)
142                         (funcallable-standard-class
143                          funcallable-standard-class-wrapper)
144                         (built-in-class built-in-class-wrapper)
145                         (structure-class structure-class-wrapper)
146                         (condition-class condition-class-wrapper)))
147              (class (or (find-class name nil)
148                         (allocate-standard-instance wrapper))))
149         (setf (find-class name) class)))
150     (dolist (definition *early-class-definitions*)
151       (let ((name (ecd-class-name definition))
152             (meta (ecd-metaclass definition))
153             (source (ecd-source definition))
154             (direct-supers (ecd-superclass-names definition))
155             (direct-slots  (ecd-canonical-slots definition))
156             (other-initargs (ecd-other-initargs definition)))
157         (let ((direct-default-initargs
158                (getf other-initargs :direct-default-initargs)))
159           (multiple-value-bind (slots cpl default-initargs direct-subclasses)
160               (early-collect-inheritance name)
161             (let* ((class (find-class name))
162                    (wrapper (cond ((eq class slot-class)
163                                    slot-class-wrapper)
164                                   ((eq class standard-class)
165                                    standard-class-wrapper)
166                                   ((eq class funcallable-standard-class)
167                                    funcallable-standard-class-wrapper)
168                                   ((eq class standard-direct-slot-definition)
169                                    standard-direct-slot-definition-wrapper)
170                                   ((eq class
171                                        standard-effective-slot-definition)
172                                    standard-effective-slot-definition-wrapper)
173                                   ((eq class built-in-class)
174                                    built-in-class-wrapper)
175                                   ((eq class structure-class)
176                                    structure-class-wrapper)
177                                   ((eq class condition-class)
178                                    condition-class-wrapper)
179                                   ((eq class class-eq-specializer)
180                                    class-eq-specializer-wrapper)
181                                   ((eq class standard-generic-function)
182                                    standard-generic-function-wrapper)
183                                   (t
184                                    (boot-make-wrapper (length slots) name))))
185                    (proto nil))
186               (when (eq name t) (setq *the-wrapper-of-t* wrapper))
187               (set (make-class-symbol name) class)
188               (dolist (slot slots)
189                 (unless (eq (getf slot :allocation :instance) :instance)
190                   (error "Slot allocation ~S is not supported in bootstrap."
191                          (getf slot :allocation))))
192
193               (when (typep wrapper 'wrapper)
194                 (setf (wrapper-instance-slots-layout wrapper)
195                       (mapcar #'canonical-slot-name slots))
196                 (setf (wrapper-class-slots wrapper)
197                       ()))
198
199               (setq proto (if (eq meta 'funcallable-standard-class)
200                               (allocate-funcallable-instance wrapper)
201                               (allocate-standard-instance wrapper)))
202
203               (setq direct-slots
204                     (!bootstrap-make-slot-definitions
205                      name class direct-slots
206                      standard-direct-slot-definition-wrapper nil))
207               (setq slots
208                     (!bootstrap-make-slot-definitions
209                      name class slots
210                      standard-effective-slot-definition-wrapper t))
211
212               (case meta
213                 ((standard-class funcallable-standard-class)
214                  (!bootstrap-initialize-class
215                   meta
216                   class name class-eq-specializer-wrapper source
217                   direct-supers direct-subclasses cpl wrapper proto
218                   direct-slots slots direct-default-initargs default-initargs))
219                 (built-in-class         ; *the-class-t*
220                  (!bootstrap-initialize-class
221                   meta
222                   class name class-eq-specializer-wrapper source
223                   direct-supers direct-subclasses cpl wrapper proto))
224                 (slot-class             ; *the-class-slot-object*
225                  (!bootstrap-initialize-class
226                   meta
227                   class name class-eq-specializer-wrapper source
228                   direct-supers direct-subclasses cpl wrapper proto))
229                 (structure-class        ; *the-class-structure-object*
230                  (!bootstrap-initialize-class
231                   meta
232                   class name class-eq-specializer-wrapper source
233                   direct-supers direct-subclasses cpl wrapper))
234                 (condition-class
235                  (!bootstrap-initialize-class
236                   meta
237                   class name class-eq-specializer-wrapper source
238                   direct-supers direct-subclasses cpl wrapper))))))))
239
240     (let* ((smc-class (find-class 'standard-method-combination))
241            (smc-wrapper (!bootstrap-get-slot 'standard-class
242                                              smc-class
243                                              'wrapper))
244            (smc (allocate-standard-instance smc-wrapper)))
245       (flet ((set-slot (name value)
246                (!bootstrap-set-slot 'standard-method-combination
247                                     smc
248                                     name
249                                     value)))
250         (set-slot 'source *load-pathname*)
251         (set-slot 'type 'standard)
252         (set-slot 'documentation "The standard method combination.")
253         (set-slot 'options ()))
254       (setq *standard-method-combination* smc))))
255
256 ;;; Initialize a class metaobject.
257 (defun !bootstrap-initialize-class
258        (metaclass-name class name
259         class-eq-wrapper source direct-supers direct-subclasses cpl wrapper
260         &optional
261         (proto nil proto-p)
262         direct-slots slots direct-default-initargs default-initargs)
263   (flet ((classes (names) (mapcar #'find-class names))
264          (set-slot (slot-name value)
265            (!bootstrap-set-slot metaclass-name class slot-name value)))
266     (set-slot 'name name)
267     (set-slot 'finalized-p t)
268     (set-slot 'source source)
269     (set-slot 'type (if (eq class (find-class t))
270                         t
271                         ;; FIXME: Could this just be CLASS instead
272                         ;; of `(CLASS ,CLASS)? If not, why not?
273                         ;; (See also similar expression in
274                         ;; SHARED-INITIALIZE :BEFORE (CLASS).)
275                         `(class ,class)))
276     (set-slot 'class-eq-specializer
277               (let ((spec (allocate-standard-instance class-eq-wrapper)))
278                 (!bootstrap-set-slot 'class-eq-specializer spec 'type
279                                      `(class-eq ,class))
280                 (!bootstrap-set-slot 'class-eq-specializer spec 'object
281                                      class)
282                 spec))
283     (set-slot 'class-precedence-list (classes cpl))
284     (set-slot 'cpl-available-p t)
285     (set-slot 'can-precede-list (classes (cdr cpl)))
286     (set-slot 'incompatible-superclass-list nil)
287     (set-slot 'direct-superclasses (classes direct-supers))
288     (set-slot 'direct-subclasses (classes direct-subclasses))
289     (set-slot 'direct-methods (cons nil nil))
290     (set-slot 'wrapper wrapper)
291     (set-slot 'predicate-name (or (cadr (assoc name *early-class-predicates*))
292                                   (make-class-predicate-name name)))
293     (set-slot 'documentation nil)
294     (set-slot 'plist
295               `(,@(and direct-default-initargs
296                        `(direct-default-initargs ,direct-default-initargs))
297                 ,@(and default-initargs
298                        `(default-initargs ,default-initargs))))
299     (when (memq metaclass-name '(standard-class funcallable-standard-class
300                                  structure-class condition-class
301                                  slot-class))
302       (set-slot 'direct-slots direct-slots)
303       (set-slot 'slots slots))
304
305     ;; For all direct superclasses SUPER of CLASS, make sure CLASS is
306     ;; a direct subclass of SUPER.  Note that METACLASS-NAME doesn't
307     ;; matter here for the slot DIRECT-SUBCLASSES, since every class
308     ;; inherits the slot from class CLASS.
309     (dolist (super direct-supers)
310       (let* ((super (find-class super))
311              (subclasses (!bootstrap-get-slot metaclass-name super
312                                               'direct-subclasses)))
313         (cond ((eq +slot-unbound+ subclasses)
314                (!bootstrap-set-slot metaclass-name super 'direct-subclasses
315                                     (list class)))
316               ((not (memq class subclasses))
317                (!bootstrap-set-slot metaclass-name super 'direct-subclasses
318                                     (cons class subclasses))))))
319
320     (case metaclass-name
321       (structure-class
322        (let ((constructor-sym '|STRUCTURE-OBJECT class constructor|))
323          (set-slot 'predicate-name (or (cadr (assoc name
324                                                     *early-class-predicates*))
325                                        (make-class-predicate-name name)))
326          (set-slot 'defstruct-form
327                    `(defstruct (structure-object (:constructor
328                                                   ,constructor-sym)
329                                                  (:copier nil))))
330          (set-slot 'defstruct-constructor constructor-sym)
331          (set-slot 'from-defclass-p t)
332          (set-slot 'plist nil)
333          (set-slot 'prototype (funcall constructor-sym))))
334       (condition-class
335        (set-slot 'prototype (make-condition name)))
336       (t
337        (set-slot 'prototype
338                  (if proto-p proto (allocate-standard-instance wrapper)))))
339     class))
340
341 (defun !bootstrap-make-slot-definitions (name class slots wrapper effective-p)
342   (let ((index -1))
343     (mapcar (lambda (slot)
344               (incf index)
345               (!bootstrap-make-slot-definition
346                name class slot wrapper effective-p index))
347             slots)))
348
349 (defun !bootstrap-make-slot-definition
350     (name class slot wrapper effective-p index)
351   (let* ((slotd-class-name (if effective-p
352                                'standard-effective-slot-definition
353                                'standard-direct-slot-definition))
354          (slotd (allocate-standard-instance wrapper))
355          (slot-name (getf slot :name)))
356     (flet ((get-val (name) (getf slot name))
357            (set-val (name val)
358                     (!bootstrap-set-slot slotd-class-name slotd name val)))
359       (set-val 'name         slot-name)
360       (set-val 'initform     (get-val :initform))
361       (set-val 'initfunction (get-val :initfunction))
362       (set-val 'initargs     (get-val :initargs))
363       (set-val 'readers      (get-val :readers))
364       (set-val 'writers      (get-val :writers))
365       (set-val 'allocation   :instance)
366       (set-val 'type         (or (get-val :type) t))
367       (set-val 'documentation (or (get-val :documentation) ""))
368       (set-val 'class   class)
369       (when effective-p
370         (set-val 'location index)
371         (let ((fsc-p nil))
372           (set-val 'reader-function (make-optimized-std-reader-method-function
373                                      fsc-p nil slot-name index))
374           (set-val 'writer-function (make-optimized-std-writer-method-function
375                                      fsc-p nil slot-name index))
376           (set-val 'boundp-function (make-optimized-std-boundp-method-function
377                                      fsc-p nil slot-name index)))
378         (set-val 'accessor-flags 7)
379         (let ((table (or (gethash slot-name *name->class->slotd-table*)
380                          (setf (gethash slot-name *name->class->slotd-table*)
381                                (make-hash-table :test 'eq :size 5)))))
382           (setf (gethash class table) slotd)))
383       (when (and (eq name 'standard-class)
384                  (eq slot-name 'slots) effective-p)
385         (setq *the-eslotd-standard-class-slots* slotd))
386       (when (and (eq name 'funcallable-standard-class)
387                  (eq slot-name 'slots) effective-p)
388         (setq *the-eslotd-funcallable-standard-class-slots* slotd))
389       slotd)))
390
391 (defun !bootstrap-accessor-definitions (early-p)
392   (let ((*early-p* early-p))
393     (dolist (definition *early-class-definitions*)
394       (let ((name (ecd-class-name definition))
395             (meta (ecd-metaclass definition)))
396         (unless (eq meta 'built-in-class)
397           (let ((direct-slots  (ecd-canonical-slots definition)))
398             (dolist (slotd direct-slots)
399               (let ((slot-name (getf slotd :name))
400                     (readers (getf slotd :readers))
401                     (writers (getf slotd :writers)))
402                 (!bootstrap-accessor-definitions1
403                  name
404                  slot-name
405                  readers
406                  writers
407                  nil)
408                 (!bootstrap-accessor-definitions1
409                  'slot-object
410                  slot-name
411                  (list (slot-reader-name slot-name))
412                  (list (slot-writer-name slot-name))
413                  (list (slot-boundp-name slot-name)))))))))))
414
415 (defun !bootstrap-accessor-definition (class-name accessor-name slot-name type)
416   (multiple-value-bind (accessor-class make-method-function arglist specls doc)
417       (ecase type
418         (reader (values 'standard-reader-method
419                         #'make-std-reader-method-function
420                         (list class-name)
421                         (list class-name)
422                         "automatically generated reader method"))
423         (writer (values 'standard-writer-method
424                         #'make-std-writer-method-function
425                         (list 'new-value class-name)
426                         (list t class-name)
427                         "automatically generated writer method"))
428         (boundp (values 'standard-boundp-method
429                         #'make-std-boundp-method-function
430                         (list class-name)
431                         (list class-name)
432                         "automatically generated boundp method")))
433     (let ((gf (ensure-generic-function accessor-name
434                                        :lambda-list arglist)))
435       (if (find specls (early-gf-methods gf)
436                 :key #'early-method-specializers
437                 :test 'equal)
438           (unless (assoc accessor-name *!generic-function-fixups*
439                          :test #'equal)
440             (update-dfun gf))
441           (add-method gf
442                       (make-a-method accessor-class
443                                      ()
444                                      arglist specls
445                                      (funcall make-method-function
446                                               class-name slot-name)
447                                      doc
448                                      slot-name))))))
449
450 (defun !bootstrap-accessor-definitions1 (class-name
451                                         slot-name
452                                         readers
453                                         writers
454                                         boundps)
455   (flet ((do-reader-definition (reader)
456            (!bootstrap-accessor-definition class-name
457                                            reader
458                                            slot-name
459                                            'reader))
460          (do-writer-definition (writer)
461            (!bootstrap-accessor-definition class-name
462                                            writer
463                                            slot-name
464                                            'writer))
465          (do-boundp-definition (boundp)
466            (!bootstrap-accessor-definition class-name
467                                            boundp
468                                            slot-name
469                                            'boundp)))
470     (dolist (reader readers) (do-reader-definition reader))
471     (dolist (writer writers) (do-writer-definition writer))
472     (dolist (boundp boundps) (do-boundp-definition boundp))))
473
474 (defun !bootstrap-class-predicates (early-p)
475   (let ((*early-p* early-p))
476     (dolist (definition *early-class-definitions*)
477       (let* ((name (ecd-class-name definition))
478              (class (find-class name)))
479         (setf (find-class-predicate name)
480               (make-class-predicate class (class-predicate-name class)))))))
481
482 (defun !bootstrap-built-in-classes ()
483
484   ;; First make sure that all the supers listed in
485   ;; *BUILT-IN-CLASS-LATTICE* are themselves defined by
486   ;; *BUILT-IN-CLASS-LATTICE*. This is just to check for typos and
487   ;; other sorts of brainos.
488   (dolist (e *built-in-classes*)
489     (dolist (super (cadr e))
490       (unless (or (eq super t)
491                   (assq super *built-in-classes*))
492         (error "in *BUILT-IN-CLASSES*: ~S has ~S as a super,~%~
493                 but ~S is not itself a class in *BUILT-IN-CLASSES*."
494                (car e) super super))))
495
496   ;; In the first pass, we create a skeletal object to be bound to the
497   ;; class name.
498   (let* ((built-in-class (find-class 'built-in-class))
499          (built-in-class-wrapper (class-wrapper built-in-class)))
500     (dolist (e *built-in-classes*)
501       (let ((class (allocate-standard-instance built-in-class-wrapper)))
502         (setf (find-class (car e)) class))))
503
504   ;; In the second pass, we initialize the class objects.
505   (let ((class-eq-wrapper (class-wrapper (find-class 'class-eq-specializer))))
506     (dolist (e *built-in-classes*)
507       (destructuring-bind (name supers subs cpl prototype) e
508         (let* ((class (find-class name))
509                (lclass (find-classoid name))
510                (wrapper (classoid-layout lclass)))
511           (set (get-built-in-class-symbol name) class)
512           (set (get-built-in-wrapper-symbol name) wrapper)
513           (setf (classoid-pcl-class lclass) class)
514
515           (!bootstrap-initialize-class 'built-in-class class
516                                        name class-eq-wrapper nil
517                                        supers subs
518                                        (cons name cpl)
519                                        wrapper prototype)))))
520
521   (dolist (e *built-in-classes*)
522     (let* ((name (car e))
523            (class (find-class name)))
524       (setf (find-class-predicate name)
525             (make-class-predicate class (class-predicate-name class))))))
526 \f
527 (defmacro wrapper-of-macro (x)
528   `(layout-of ,x))
529
530 (defun class-of (x)
531   (wrapper-class* (wrapper-of-macro x)))
532
533 ;;; FIXME: We probably don't need both WRAPPER-OF and WRAPPER-OF-MACRO.
534 #-sb-fluid (declaim (inline wrapper-of))
535 (defun wrapper-of (x)
536   (wrapper-of-macro x))
537
538 (defun eval-form (form)
539   (lambda () (eval form)))
540
541 (defun ensure-non-standard-class (name &optional existing-class)
542   (flet
543       ((ensure (metaclass &optional (slots nil slotsp))
544          (let ((supers
545                 (mapcar #'classoid-name (classoid-direct-superclasses
546                                          (find-classoid name)))))
547            (if slotsp
548                (ensure-class-using-class existing-class name
549                                          :metaclass metaclass :name name
550                                          :direct-superclasses supers
551                                          :direct-slots slots)
552                (ensure-class-using-class existing-class name
553                                          :metaclass metaclass :name name
554                                          :direct-superclasses supers))))
555        (slot-initargs-from-structure-slotd (slotd)
556          (let ((accessor (structure-slotd-accessor-symbol slotd)))
557            `(:name ,(structure-slotd-name slotd)
558              :defstruct-accessor-symbol ,accessor
559              ,@(when (fboundp accessor)
560                  `(:internal-reader-function
561                    ,(structure-slotd-reader-function slotd)
562                    :internal-writer-function
563                    ,(structure-slotd-writer-function name slotd)))
564              :type ,(or (structure-slotd-type slotd) t)
565              :initform ,(structure-slotd-init-form slotd)
566              :initfunction ,(eval-form (structure-slotd-init-form slotd)))))
567        (slot-initargs-from-condition-slot (slot)
568          `(:name ,(condition-slot-name slot)
569            :initargs ,(condition-slot-initargs slot)
570            :readers ,(condition-slot-readers slot)
571            :writers ,(condition-slot-writers slot)
572            ,@(when (condition-slot-initform-p slot)
573                (let ((form-or-fun (condition-slot-initform slot)))
574                  (if (functionp form-or-fun)
575                      `(:initfunction ,form-or-fun)
576                      `(:initform ,form-or-fun
577                        :initfunction ,(lambda () form-or-fun)))))
578            :allocation ,(condition-slot-allocation slot)
579            :documentation ,(condition-slot-documentation slot))))
580     (cond ((structure-type-p name)
581            (ensure 'structure-class
582                    (mapcar #'slot-initargs-from-structure-slotd
583                            (structure-type-slot-description-list name))))
584           ((condition-type-p name)
585            (ensure 'condition-class
586                    (mapcar #'slot-initargs-from-condition-slot
587                            (condition-classoid-slots (find-classoid name)))))
588           (t
589            (error "~@<~S is not the name of a class.~@:>" name)))))
590
591 (defun ensure-defstruct-class (classoid)
592   (let ((class (classoid-pcl-class classoid)))
593     (cond (class
594            (ensure-non-standard-class (class-name class) class))
595           ((eq 'complete *boot-state*)
596            (ensure-non-standard-class (classoid-name classoid))))))
597
598 (pushnew 'ensure-defstruct-class sb-kernel::*defstruct-hooks*)
599 \f
600 (defun make-class-predicate (class name)
601   (let* ((gf (ensure-generic-function name :lambda-list '(object)))
602          (mlist (if (eq *boot-state* 'complete)
603                     (generic-function-methods gf)
604                     (early-gf-methods gf))))
605     (unless mlist
606       (unless (eq class *the-class-t*)
607         (let* ((default-method-function #'constantly-nil)
608                (default-method-initargs (list :function
609                                               default-method-function))
610                (default-method (make-a-method
611                                 'standard-method
612                                 ()
613                                 (list 'object)
614                                 (list *the-class-t*)
615                                 default-method-initargs
616                                 "class predicate default method")))
617           (setf (method-function-get default-method-function :constant-value)
618                 nil)
619           (add-method gf default-method)))
620       (let* ((class-method-function #'constantly-t)
621              (class-method-initargs (list :function
622                                           class-method-function))
623              (class-method (make-a-method 'standard-method
624                                           ()
625                                           (list 'object)
626                                           (list class)
627                                           class-method-initargs
628                                           "class predicate class method")))
629         (setf (method-function-get class-method-function :constant-value) t)
630         (add-method gf class-method)))
631     gf))
632
633 ;;; Set the inherits from CPL, and register the layout. This actually
634 ;;; installs the class in the Lisp type system.
635 (defun update-lisp-class-layout (class layout)
636   (let ((lclass (layout-classoid layout)))
637     (unless (eq (classoid-layout lclass) layout)
638       (setf (layout-inherits layout)
639               (order-layout-inherits
640                (map 'simple-vector #'class-wrapper
641                     (reverse (rest (class-precedence-list class))))))
642       (register-layout layout :invalidate t)
643
644       ;; Subclasses of formerly forward-referenced-class may be
645       ;; unknown to CL:FIND-CLASS and also anonymous. This
646       ;; functionality moved here from (SETF FIND-CLASS).
647       (let ((name (class-name class)))
648         (setf (find-classoid name) lclass
649               (classoid-name lclass) name)))))
650
651 (defun set-class-type-translation (class name)
652   (let ((classoid (find-classoid name nil)))
653     (etypecase classoid
654       (null)
655       (built-in-classoid
656        (let ((translation (built-in-classoid-translation classoid)))
657          (cond
658            (translation
659             (aver (ctype-p translation))
660             (setf (info :type :translator class)
661                   (lambda (spec) (declare (ignore spec)) translation)))
662            (t
663             (setf (info :type :translator class)
664                   (lambda (spec) (declare (ignore spec)) classoid))))))
665       (classoid
666        (setf (info :type :translator class)
667              (lambda (spec) (declare (ignore spec)) classoid))))))
668
669 (clrhash *find-class*)
670 (!bootstrap-meta-braid)
671 (!bootstrap-accessor-definitions t)
672 (!bootstrap-class-predicates t)
673 (!bootstrap-accessor-definitions nil)
674 (!bootstrap-class-predicates nil)
675 (!bootstrap-built-in-classes)
676
677 (dohash (name x *find-class*)
678         (let* ((class (find-class-from-cell name x))
679                (layout (class-wrapper class))
680                (lclass (layout-classoid layout))
681                (lclass-pcl-class (classoid-pcl-class lclass))
682                (olclass (find-classoid name nil)))
683           (if lclass-pcl-class
684               (aver (eq class lclass-pcl-class))
685               (setf (classoid-pcl-class lclass) class))
686
687           (update-lisp-class-layout class layout)
688
689           (cond (olclass
690                  (aver (eq lclass olclass)))
691                 (t
692                  (setf (find-classoid name) lclass)))
693
694           (set-class-type-translation class name)))
695
696 (setq *boot-state* 'braid)
697
698 (defmethod no-applicable-method (generic-function &rest args)
699   (error "~@<There is no applicable method for the generic function ~2I~_~S~
700           ~I~_when called with arguments ~2I~_~S.~:>"
701          generic-function
702          args))
703
704 (defmethod no-next-method ((generic-function standard-generic-function)
705                            (method standard-method) &rest args)
706   (error "~@<There is no next method for the generic function ~2I~_~S~
707           ~I~_when called from method ~2I~_~S~I~_with arguments ~2I~_~S.~:>"
708          generic-function
709          method
710          args))
711
712 ;;; An extension to the ANSI standard: in the presence of e.g. a
713 ;;; :BEFORE method, it would seem that going through
714 ;;; NO-APPLICABLE-METHOD is prohibited, as in fact there is an
715 ;;; applicable method.  -- CSR, 2002-11-15
716 (defmethod no-primary-method (generic-function &rest args)
717   (error "~@<There is no primary method for the generic function ~2I~_~S~
718           ~I~_when called with arguments ~2I~_~S.~:>"
719          generic-function
720          args))
721
722 (defmethod invalid-qualifiers ((gf generic-function)
723                                combin
724                                method)
725   (let ((qualifiers (method-qualifiers method)))
726     (let ((why (cond
727                  ((cdr qualifiers) "has too many qualifiers")
728                  (t (aver (not (member (car qualifiers)
729                                        '(:around :before :after))))
730                     "has an invalid qualifier"))))
731       (invalid-method-error
732        method
733        "The method ~S on ~S ~A.~%~
734         Standard method combination requires all methods to have one~%~
735         of the single qualifiers :AROUND, :BEFORE and :AFTER or to~%~
736         have no qualifier at all."
737        method gf why))))