ca880839a82b98d815b00ef92a81b6782701d92f
[sbcl.git] / src / pcl / ctor.lisp
1 ;;;; This file contains the optimization machinery for make-instance.
2
3 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; more information.
5
6 ;;;; This software is derived from software originally released by
7 ;;;; Gerd Moellmann.  Copyright and release statements follow.  Later
8 ;;;; modifications to the software are in the public domain and are
9 ;;;; provided with absolutely no warranty.  See the COPYING and
10 ;;;; CREDITS files for more information.
11
12 ;;; Copyright (C) 2002 Gerd Moellmann <gerd.moellmann@t-online.de>
13 ;;; All rights reserved.
14 ;;;
15 ;;; Redistribution and use in source and binary forms, with or without
16 ;;; modification, are permitted provided that the following conditions
17 ;;; are met:
18 ;;;
19 ;;; 1. Redistributions of source code must retain the above copyright
20 ;;;    notice, this list of conditions and the following disclaimer.
21 ;;; 2. Redistributions in binary form must reproduce the above copyright
22 ;;;    notice, this list of conditions and the following disclaimer in the
23 ;;;    documentation and/or other materials provided with the distribution.
24 ;;; 3. The name of the author may not be used to endorse or promote
25 ;;;    products derived from this software without specific prior written
26 ;;;    permission.
27 ;;;
28 ;;; THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
29 ;;; OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
30 ;;; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 ;;; ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE
32 ;;; LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33 ;;; CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
34 ;;; OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
35 ;;; BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
36 ;;; LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 ;;; (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
38 ;;; USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
39 ;;; DAMAGE.
40
41 ;;; ***************
42 ;;; Overview  *****
43 ;;; ***************
44 ;;;
45 ;;; Compiler macro for MAKE-INSTANCE, and load-time generation of
46 ;;; optimized instance constructor functions.
47 ;;;
48 ;;; ********************
49 ;;; Entry Points  ******
50 ;;; ********************
51 ;;;
52 ;;; UPDATE-CTORS must be called when methods are added/removed,
53 ;;; classes are changed, etc., which affect instance creation.
54 ;;;
55 ;;; PRECOMPILE-CTORS can be called to precompile constructor functions
56 ;;; for classes whose definitions are known at the time the function
57 ;;; is called.
58
59 (in-package "SB-PCL")
60
61 ;;; ******************
62 ;;; Utilities  *******
63 ;;; ******************
64
65 (defun plist-keys (plist &key test)
66   (loop for (key . more) on plist by #'cddr
67         if (null more) do
68           (error "Not a property list: ~S" plist)
69         else if (or (null test) (funcall test key))
70           collect key))
71
72 (defun plist-values (plist &key test)
73   (loop for (key . more) on plist by #'cddr
74         if (null more) do
75           (error "Not a property list: ~S" plist)
76         else if (or (null test) (funcall test (car more)))
77           collect (car more)))
78
79 (defun constant-symbol-p (form)
80   (and (constantp form)
81        (let ((constant (eval form)))
82          (and (symbolp constant)
83               (not (null (symbol-package constant)))))))
84
85 \f
86 ;;; *****************
87 ;;; CTORS   *********
88 ;;; *****************
89 ;;;
90 ;;; Ctors are funcallable instances whose initial function is a
91 ;;; function computing an optimized constructor function when called.
92 ;;; When the optimized function is computed, the function of the
93 ;;; funcallable instance is set to it.
94 ;;;
95 (!defstruct-with-alternate-metaclass ctor
96   :slot-names (function-name class-name class initargs)
97   :boa-constructor %make-ctor
98   :superclass-name pcl-funcallable-instance
99   :metaclass-name random-pcl-classoid
100   :metaclass-constructor make-random-pcl-classoid
101   :dd-type funcallable-structure
102   :runtime-type-checks-p nil)
103
104 ;;; List of all defined ctors.
105
106 (defvar *all-ctors* ())
107
108 (defun make-ctor-parameter-list (ctor)
109   (plist-values (ctor-initargs ctor) :test (complement #'constantp)))
110
111 ;;;
112 ;;; Reset CTOR to use a default function that will compute an
113 ;;; optimized constructor function when called.
114 ;;;
115 (defun install-initial-constructor (ctor &key force-p)
116   (when (or force-p (ctor-class ctor))
117     (setf (ctor-class ctor) nil)
118     (setf (funcallable-instance-fun ctor)
119           #'(instance-lambda (&rest args)
120               (install-optimized-constructor ctor)
121               (apply ctor args)))
122     (setf (%funcallable-instance-info ctor 1)
123           (ctor-function-name ctor))))
124
125 ;;;
126 ;;; Keep this a separate function for testing.
127 ;;;
128 (defun make-ctor-function-name (class-name initargs)
129   (let ((*package* *pcl-package*)
130         (*print-case* :upcase)
131         (*print-pretty* nil)
132         (*print-gensym* t))
133     (intern (format nil "CTOR ~S::~S ~S ~S"
134                     (package-name (symbol-package class-name))
135                     (symbol-name class-name)
136                     (plist-keys initargs)
137                     (plist-values initargs :test #'constantp))
138             *pcl-package*)))
139
140 ;;;
141 ;;; Keep this a separate function for testing.
142 ;;;
143 (defun ensure-ctor (function-name class-name initargs)
144   (unless (fboundp function-name)
145     (make-ctor function-name class-name initargs)))
146
147 ;;;
148 ;;; Keep this a separate function for testing.
149 ;;;
150 (defun make-ctor (function-name class-name initargs)
151   (let ((ctor (%make-ctor function-name class-name nil initargs)))
152     (push ctor *all-ctors*)
153     (setf (symbol-function function-name) ctor)
154     (install-initial-constructor ctor :force-p t)
155     ctor))
156
157 \f
158 ;;; ***********************************************
159 ;;; Compile-Time Expansion of MAKE-INSTANCE *******
160 ;;; ***********************************************
161
162 (define-compiler-macro make-instance (&whole form &rest args)
163   (declare (ignore args))
164   (or (make-instance->constructor-call form)
165       form))
166
167 (defun make-instance->constructor-call (form)
168   (destructuring-bind (fn class-name &rest args) form
169     (declare (ignore fn))
170     (flet (;;
171            ;; Return the name of parameter number I of a constructor
172            ;; function.
173            (parameter-name (i)
174              (let ((ps #(.p0. .p1. .p2. .p3. .p4. .p5.)))
175                (if (array-in-bounds-p ps i)
176                    (aref ps i)
177                    (intern (format nil ".P~D." i) *pcl-package*))))
178            ;;
179            ;; Check if CLASS-NAME is a constant symbol.  Give up if
180            ;; not.
181            (check-class ()
182              (unless (and class-name (constant-symbol-p class-name))
183                (return-from make-instance->constructor-call nil)))
184            ;;
185            ;; Check if ARGS are suitable for an optimized constructor.
186            ;; Return NIL from the outer function if not.
187            (check-args ()
188              (loop for (key . more) on args by #'cddr do
189                      (when (or (null more)
190                                (not (constant-symbol-p key))
191                                (eq :allow-other-keys (eval key)))
192                        (return-from make-instance->constructor-call nil)))))
193       (check-class)
194       (check-args)
195       ;;
196       ;; Collect a plist of initargs and constant values/parameter names
197       ;; in INITARGS.  Collect non-constant initialization forms in
198       ;; VALUE-FORMS.
199       (multiple-value-bind (initargs value-forms)
200           (loop for (key value) on args by #'cddr and i from 0
201                 collect (eval key) into initargs
202                 if (constantp value)
203                   collect value into initargs
204                 else
205                   collect (parameter-name i) into initargs
206                   and collect value into value-forms
207                 finally
208                   (return (values initargs value-forms)))
209         (let* ((class-name (eval class-name))
210                (function-name (make-ctor-function-name class-name initargs)))
211           ;;
212           ;; Prevent compiler warnings for calling the ctor.
213           (proclaim-as-fun-name function-name)
214           (note-name-defined function-name :function)
215           (when (eq (info :function :where-from function-name) :assumed)
216             (setf (info :function :where-from function-name) :defined)
217             (when (info :function :assumed-type function-name)
218               (setf (info :function :assumed-type function-name) nil)))
219           ;;
220           ;; Return code constructing a ctor at load time, which, when
221           ;; called, will set its funcallable instance function to an
222           ;; optimized constructor function.
223           `(let ((.x. (load-time-value
224                        (ensure-ctor ',function-name ',class-name ',initargs))))
225              (declare (ignore .x.))
226              ;;; ??? check if this is worth it.
227              (declare
228               (ftype (or (function ,(make-list (length value-forms)
229                                                :initial-element t)
230                                    t)
231                          (function (&rest t) t))
232                      ,function-name))
233              (,function-name ,@value-forms)))))))
234
235 \f
236 ;;; **************************************************
237 ;;; Load-Time Constructor Function Generation  *******
238 ;;; **************************************************
239
240 ;;;
241 ;;; The system-supplied primary INITIALIZE-INSTANCE and
242 ;;; SHARED-INITIALIZE methods.  One cannot initialized these variables
243 ;;; to the right values here because said functions don't exist yet
244 ;;; when this file is first loaded.
245 ;;;
246 (defvar *the-system-ii-method* nil)
247 (defvar *the-system-si-method* nil)
248
249 (defun install-optimized-constructor (ctor)
250   (let ((class (find-class (ctor-class-name ctor))))
251     (unless (class-finalized-p class)
252       (finalize-inheritance class))
253     (setf (ctor-class ctor) class)
254     (pushnew ctor (plist-value class 'ctors))
255     (setf (funcallable-instance-fun ctor)
256           ;; KLUDGE: Gerd here has the equivalent of (COMPILE NIL
257           ;; (CONSTRUCTOR-FUNCTION-FORM)), but SBCL's COMPILE doesn't
258           ;; deal with INSTANCE-LAMBDA expressions, only with LAMBDA
259           ;; expressions.  The below should be equivalent, since we
260           ;; have a compiler-only implementation.
261           (eval `(function ,(constructor-function-form ctor))))))
262               
263 (defun constructor-function-form (ctor)
264   (let* ((class (ctor-class ctor))
265          (proto (class-prototype class))
266          (make-instance-methods
267           (compute-applicable-methods #'make-instance (list class)))
268          (allocate-instance-methods
269           (compute-applicable-methods #'allocate-instance (list class)))
270          (ii-methods
271           (compute-applicable-methods #'initialize-instance (list proto)))
272          (si-methods
273           (compute-applicable-methods #'shared-initialize (list proto t))))
274     ;; Cannot initialize these variables earlier because the generic
275     ;; functions don't exist when PCL is built.
276     (when (null *the-system-si-method*)
277       (setq *the-system-si-method*
278             (find-method #'shared-initialize
279                          () (list *the-class-slot-object* *the-class-t*)))
280       (setq *the-system-ii-method*
281             (find-method #'initialize-instance
282                          () (list *the-class-slot-object*))))
283     ;; Note that when there are user-defined applicable methods on
284     ;; MAKE-INSTANCE and/or ALLOCATE-INSTANCE, these will show up
285     ;; together with the system-defined ones in what
286     ;; COMPUTE-APPLICABLE-METHODS returns.
287     (or (and (not (structure-class-p class))
288              (not (condition-class-p class))
289              (null (cdr make-instance-methods))
290              (null (cdr allocate-instance-methods))
291              (null (check-initargs-1 class (plist-keys (ctor-initargs ctor))
292                                      (append ii-methods si-methods) nil nil))
293              (not (around-or-nonstandard-primary-method-p
294                    ii-methods *the-system-ii-method*))
295              (not (around-or-nonstandard-primary-method-p
296                    si-methods *the-system-si-method*))
297              (optimizing-generator ctor ii-methods si-methods))
298         (fallback-generator ctor ii-methods si-methods))))
299
300 (defun around-or-nonstandard-primary-method-p
301     (methods &optional standard-method)
302   (loop with primary-checked-p = nil
303         for method in methods
304         as qualifiers = (method-qualifiers method)
305         when (or (eq :around (car qualifiers))
306                  (and (null qualifiers)
307                       (not primary-checked-p)
308                       (not (null standard-method))
309                       (not (eq standard-method method))))
310           return t
311         when (null qualifiers) do
312           (setq primary-checked-p t)))
313
314 (defun fallback-generator (ctor ii-methods si-methods)
315   (declare (ignore ii-methods si-methods))
316   `(instance-lambda ,(make-ctor-parameter-list ctor)
317      (make-instance ,(ctor-class ctor) ,@(ctor-initargs ctor))))
318
319 (defun optimizing-generator (ctor ii-methods si-methods)
320   (multiple-value-bind (body before-method-p)
321       (fake-initialization-emf ctor ii-methods si-methods)
322     `(instance-lambda ,(make-ctor-parameter-list ctor)
323        (declare #.*optimize-speed*)
324        ,(wrap-in-allocate-forms ctor body before-method-p))))
325
326 ;;;
327 ;;; Return a form wrapped around BODY that allocates an instance
328 ;;; constructed by CTOR.  BEFORE-METHOD-P set means we have to run
329 ;;; before-methods, in which case we initialize instance slots to
330 ;;; +SLOT-UNBOUND+.  The resulting form binds the local variables
331 ;;; .INSTANCE. to the instance, and .SLOTS. to the instance's slot
332 ;;; vector around BODY.
333 ;;;
334 (defun wrap-in-allocate-forms (ctor body before-method-p)
335   (let* ((class (ctor-class ctor))
336          (wrapper (class-wrapper class))
337          (allocation-function (raw-instance-allocator class))
338          (slots-fetcher (slots-fetcher class)))
339     (if (eq allocation-function 'allocate-standard-instance)
340         `(let ((.instance. (%make-standard-instance nil
341                                                     (get-instance-hash-code)))
342                (.slots. (make-array
343                          ,(layout-length wrapper)
344                          ,@(when before-method-p
345                              '(:initial-element +slot-unbound+)))))
346            (setf (std-instance-wrapper .instance.) ,wrapper)
347            (setf (std-instance-slots .instance.) .slots.)
348            ,body
349            .instance.)
350         `(let* ((.instance. (,allocation-function ,wrapper))
351                 (.slots. (,slots-fetcher .instance.)))
352            ,body
353            .instance.))))
354
355 ;;;
356 ;;; Return a form for invoking METHOD with arguments from ARGS.  As
357 ;;; can be seen in METHOD-FUNCTION-FROM-FAST-FUNCTION, method
358 ;;; functions look like (LAMBDA (ARGS NEXT-METHODS) ...).  We could
359 ;;; call fast method functions directly here, but benchmarks show that
360 ;;; there's no speed to gain, so lets avoid the hair here.
361 ;;;
362 (defmacro invoke-method (method args)
363   `(funcall ,(method-function method) ,args ()))
364
365 ;;;
366 ;;; Return a form that is sort of an effective method comprising all
367 ;;; calls to INITIALIZE-INSTANCE and SHARED-INITIALIZE that would
368 ;;; normally have taken place when calling MAKE-INSTANCE.
369 ;;;
370 (defun fake-initialization-emf (ctor ii-methods si-methods)
371   (multiple-value-bind (ii-around ii-before ii-primary ii-after)
372       (standard-sort-methods ii-methods)
373     (declare (ignore ii-primary))
374     (multiple-value-bind (si-around si-before si-primary si-after)
375         (standard-sort-methods si-methods)
376       (declare (ignore si-primary))
377       (aver (and (null ii-around) (null si-around)))
378       (let ((initargs (ctor-initargs ctor))
379             (slot-inits (slot-init-forms ctor (or ii-before si-before))))
380         (values
381          `(let (,@(when (or ii-before ii-after)
382                    `((.ii-args. (list .instance. ,@initargs))))
383                 ,@(when (or si-before si-after)
384                    `((.si-args. (list .instance. t ,@initargs)))))
385             ,@(loop for method in ii-before
386                     collect `(invoke-method ,method .ii-args.))
387             ,@(loop for method in si-before
388                     collect `(invoke-method ,method .si-args.))
389             ,slot-inits
390             ,@(loop for method in si-after
391                     collect `(invoke-method ,method .si-args.))
392             ,@(loop for method in ii-after
393                     collect `(invoke-method ,method .ii-args.)))
394          (or ii-before si-before))))))
395
396 ;;;
397 ;;; Return four values from APPLICABLE-METHODS: around methods, before
398 ;;; methods, the applicable primary method, and applicable after
399 ;;; methods.  Before and after methods are sorted in the order they
400 ;;; must be called.
401 ;;;
402 (defun standard-sort-methods (applicable-methods)
403   (loop for method in applicable-methods
404         as qualifiers = (method-qualifiers method)
405         if (null qualifiers)
406           collect method into primary
407         else if (eq :around (car qualifiers))
408           collect method into around
409         else if (eq :after (car qualifiers))
410           collect method into after
411         else if (eq :before (car qualifiers))
412           collect method into before
413         finally
414           (return (values around before (first primary) (reverse after)))))
415
416 ;;;
417 ;;; Return a form initializing instance and class slots of an object
418 ;;; costructed by CTOR.  The variable .SLOTS. is assumed to bound to
419 ;;; the instance's slot vector.  BEFORE-METHOD-P T means
420 ;;; before-methods will be called, which means that 1) other code will
421 ;;; initialize instance slots to +SLOT-UNBOUND+ before the
422 ;;; before-methods are run, and that we have to check if these
423 ;;; before-methods have set slots.
424 ;;;
425 (defun slot-init-forms (ctor before-method-p)
426   (let* ((class (ctor-class ctor))
427          (initargs (ctor-initargs ctor))
428          (initkeys (plist-keys initargs))
429          (slot-vector
430           (make-array (layout-length (class-wrapper class))
431                       :initial-element nil))
432          (class-inits ())
433          (default-inits ())
434          (default-initargs (class-default-initargs class))
435          (initarg-locations
436           (compute-initarg-locations
437            class (append initkeys (mapcar #'car default-initargs)))))
438     (labels ((initarg-locations (initarg)
439                (cdr (assoc initarg initarg-locations :test #'eq)))
440              (initializedp (location)
441                (cond
442                  ((consp location)
443                   (assoc location class-inits :test #'eq))
444                  ((integerp location)
445                   (not (null (aref slot-vector location))))
446                  (t (bug "Weird location in ~S" 'slot-init-forms))))
447              (class-init (location type val)
448                (aver (consp location))
449                (unless (initializedp location)
450                  (push (list location type val) class-inits)))
451              (instance-init (location type val)
452                (aver (integerp location))
453                (unless (initializedp location)
454                  (setf (aref slot-vector location) (list type val))))
455              (default-init-var-name (i)
456                (let ((ps #(.d0. .d1. .d2. .d3. .d4. .d5.)))
457                  (if (array-in-bounds-p ps i)
458                      (aref ps i)
459                      (intern (format nil ".D~D." i) *pcl-package*)))))
460       ;; Loop over supplied initargs and values and record which
461       ;; instance and class slots they initialize.
462       (loop for (key value) on initargs by #'cddr
463             as locations = (initarg-locations key) do
464               (if (constantp value)
465                   (dolist (location locations)
466                     (if (consp location)
467                         (class-init location 'constant value)
468                         (instance-init location 'constant value)))
469                     (dolist (location locations)
470                       (if (consp location)
471                           (class-init location 'param value)
472                           (instance-init location 'param value)))))
473       ;; Loop over default initargs of the class, recording
474       ;; initializations of slots that have not been initialized
475       ;; above.  Default initargs which are not in the supplied
476       ;; initargs are treated as if they were appended to supplied
477       ;; initargs, that is, their values must be evaluated even
478       ;; if not actually used for initializing a slot.
479       (loop for (key initfn initform) in default-initargs and i from 0
480             unless (member key initkeys :test #'eq) do
481               (let* ((type (if (constantp initform) 'constant 'var))
482                      (init (if (eq type 'var) initfn initform)))
483                 (when (eq type 'var)
484                   (let ((init-var (default-init-var-name i)))
485                     (setq init init-var)
486                     (push (cons init-var initfn) default-inits)))
487                 (dolist (location (initarg-locations key))
488                   (if (consp location)
489                       (class-init location type init)
490                       (instance-init location type init)))))
491       ;; Loop over all slots of the class, filling in the rest from
492       ;; slot initforms.
493       (loop for slotd in (class-slots class)
494             as location = (slot-definition-location slotd)
495             as allocation = (slot-definition-allocation slotd)
496             as initfn = (slot-definition-initfunction slotd)
497             as initform = (slot-definition-initform slotd) do
498               (unless (or (eq allocation :class)
499                           (null initfn)
500                           (initializedp location))
501                 (if (constantp initform)
502                     (instance-init location 'initform initform)
503                     (instance-init location 'initform/initfn initfn))))
504       ;; Generate the forms for initializing instance and class slots.
505       (let ((instance-init-forms
506              (loop for slot-entry across slot-vector and i from 0
507                    as (type value) = slot-entry collect
508                      (ecase type
509                        ((nil)
510                         (unless before-method-p
511                           `(setf (clos-slots-ref .slots. ,i) +slot-unbound+)))
512                        ((param var)
513                         `(setf (clos-slots-ref .slots. ,i) ,value))
514                        (initfn
515                         `(setf (clos-slots-ref .slots. ,i) (funcall ,value)))
516                        (initform/initfn
517                         (if before-method-p
518                             `(when (eq (clos-slots-ref .slots. ,i)
519                                        +slot-unbound+)
520                                (setf (clos-slots-ref .slots. ,i)
521                                      (funcall ,value)))
522                             `(setf (clos-slots-ref .slots. ,i)
523                                    (funcall ,value))))
524                        (initform
525                         (if before-method-p
526                             `(when (eq (clos-slots-ref .slots. ,i)
527                                        +slot-unbound+)
528                                (setf (clos-slots-ref .slots. ,i)
529                                      ',(eval value)))
530                             `(setf (clos-slots-ref .slots. ,i)
531                                    ',(eval value))))
532                        (constant
533                         `(setf (clos-slots-ref .slots. ,i) ',(eval value))))))
534             (class-init-forms
535              (loop for (location type value) in class-inits collect
536                      `(setf (cdr ',location)
537                             ,(ecase type
538                                (constant `',(eval value))
539                                ((param var) `,value)
540                                (initfn `(funcall ,value)))))))
541         (multiple-value-bind (vars bindings)
542             (loop for (var . initfn) in (nreverse default-inits)
543                   collect var into vars
544                   collect `(,var (funcall ,initfn)) into bindings
545                   finally (return (values vars bindings)))
546           `(let ,bindings
547              (declare (ignorable ,@vars))
548              ,@(delete nil instance-init-forms)
549              ,@class-init-forms))))))
550
551 ;;;
552 ;;; Return an alist of lists (KEY LOCATION ...) telling, for each
553 ;;; key in INITKEYS, which locations the initarg initializes.
554 ;;; CLASS is the class of the instance being initialized.
555 ;;;
556 (defun compute-initarg-locations (class initkeys)
557   (loop with slots = (class-slots class)
558         for key in initkeys collect
559           (loop for slot in slots
560                 if (memq key (slot-definition-initargs slot))
561                   collect (slot-definition-location slot) into locations
562                 else
563                   collect slot into remaining-slots
564                 finally
565                   (setq slots remaining-slots)
566                   (return (cons key locations)))))
567
568 \f
569 ;;; *******************************
570 ;;; External Entry Points  ********
571 ;;; *******************************
572
573 (defun update-ctors (reason &key class name generic-function method)
574   (labels ((reset (class &optional ri-cache-p (ctorsp t))
575              (when ctorsp
576                (dolist (ctor (plist-value class 'ctors))
577                  (install-initial-constructor ctor)))
578              (when ri-cache-p
579                (setf (plist-value class 'ri-initargs) ()))
580              (dolist (subclass (class-direct-subclasses class))
581                (reset subclass ri-cache-p ctorsp))))
582     (ecase reason
583       ;;
584       ;; CLASS must have been specified.
585       (finalize-inheritance
586        (reset class t))
587       ;;
588       ;; NAME must have been specified.
589       (setf-find-class
590        (loop for ctor in *all-ctors*
591              when (eq (ctor-class-name ctor) name) do
592              (when (ctor-class ctor)
593                (reset (ctor-class ctor)))
594              (loop-finish)))
595       ;;
596       ;; GENERIC-FUNCTION and METHOD must have been specified.
597       ((add-method remove-method)
598        (flet ((class-of-1st-method-param (method)
599                 (type-class (first (method-specializers method)))))
600          (case (generic-function-name generic-function)
601            ((make-instance allocate-instance
602              initialize-instance shared-initialize)
603             (reset (class-of-1st-method-param method) t t))
604            ((reinitialize-instance)
605             (reset (class-of-1st-method-param method) t nil))))))))
606
607 (defun precompile-ctors ()
608   (dolist (ctor *all-ctors*)
609     (when (null (ctor-class ctor))
610       (let ((class (find-class (ctor-class-name ctor) nil)))
611         (when (and class (class-finalized-p class))
612           (install-optimized-constructor ctor))))))
613
614 (defun check-ri-initargs (instance initargs)
615   (let* ((class (class-of instance))
616          (keys (plist-keys initargs))
617          (cached (assoc keys (plist-value class 'ri-initargs)
618                         :test #'equal))
619          (invalid-keys
620           (if (consp cached)
621               (cdr cached)
622               (let ((invalid
623                      ;; FIXME: give CHECK-INITARGS-1 and friends a
624                      ;; more mnemonic name and (possibly) a nicer,
625                      ;; more orthogonal interface.
626                      (check-initargs-1
627                       class initargs
628                       (list (list* 'reinitialize-instance instance initargs)
629                             (list* 'shared-initialize instance nil initargs))
630                       t nil)))
631                 (setf (plist-value class 'ri-initargs)
632                       (acons keys invalid cached))
633                 invalid))))
634     (when invalid-keys
635       (error 'initarg-error :class class :initargs invalid-keys))))
636
637 ;;; end of ctor.lisp