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