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