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