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