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