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