1 ;;;; This file contains the optimization machinery for make-instance.
3 ;;;; This software is part of the SBCL system. See the README file for
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.
12 ;;; Copyright (C) 2002 Gerd Moellmann <gerd.moellmann@t-online.de>
13 ;;; All rights reserved.
15 ;;; Redistribution and use in source and binary forms, with or without
16 ;;; modification, are permitted provided that the following conditions
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
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
45 ;;; Compiler macro for MAKE-INSTANCE, and load-time generation of
46 ;;; optimized instance constructor functions.
48 ;;; ********************
49 ;;; Entry Points ******
50 ;;; ********************
52 ;;; UPDATE-CTORS must be called when methods are added/removed,
53 ;;; classes are changed, etc., which affect instance creation.
55 ;;; PRECOMPILE-CTORS can be called to precompile constructor functions
56 ;;; for classes whose definitions are known at the time the function
61 ;;; ******************
63 ;;; ******************
65 (defun quote-plist-keys (plist)
66 (loop for (key . more) on plist by #'cddr
68 (error "Not a property list: ~S" plist)
71 and collect (car more)))
73 (defun plist-keys (plist &key test)
74 (loop for (key . more) on plist by #'cddr
76 (error "Not a property list: ~S" plist)
77 else if (or (null test) (funcall test key))
80 (defun plist-values (plist &key test)
81 (loop for (key . more) on plist by #'cddr
83 (error "Not a property list: ~S" plist)
84 else if (or (null test) (funcall test (car more)))
87 (defun constant-symbol-p (form)
89 (let ((constant (constant-form-value form)))
90 (and (symbolp constant)
91 (not (null (symbol-package constant)))))))
93 ;;; somewhat akin to DEFAULT-INITARGS (SLOT-CLASS T T), but just
94 ;;; collecting the defaulted initargs for the call.
95 (defun ctor-default-initkeys (supplied-initargs class-default-initargs)
96 (loop for (key) in class-default-initargs
97 when (eq (getf supplied-initargs key '.not-there.) '.not-there.)
100 ;;; *****************
102 ;;; *****************
104 ;;; Ctors are funcallable instances whose initial function is a
105 ;;; function computing an optimized constructor function when called.
106 ;;; When the optimized function is computed, the function of the
107 ;;; funcallable instance is set to it.
109 (!defstruct-with-alternate-metaclass ctor
110 :slot-names (function-name class-name class initargs safe-p)
111 :boa-constructor %make-ctor
112 :superclass-name function
113 :metaclass-name static-classoid
114 :metaclass-constructor make-static-classoid
115 :dd-type funcallable-structure
116 :runtime-type-checks-p nil)
118 ;;; List of all defined ctors.
120 (defvar *all-ctors* ())
122 (defun make-ctor-parameter-list (ctor)
123 (plist-values (ctor-initargs ctor) :test (complement #'constantp)))
125 ;;; Reset CTOR to use a default function that will compute an
126 ;;; optimized constructor function when called.
127 (defun install-initial-constructor (ctor &key force-p)
128 (when (or force-p (ctor-class ctor))
129 (setf (ctor-class ctor) nil)
130 (setf (funcallable-instance-fun ctor)
131 #'(lambda (&rest args)
132 (install-optimized-constructor ctor)
134 (setf (%funcallable-instance-info ctor 1)
135 (ctor-function-name ctor))))
137 (defun make-ctor-function-name (class-name initargs safe-code-p)
138 (list* 'ctor class-name safe-code-p initargs))
140 ;;; Keep this a separate function for testing.
141 (defun ensure-ctor (function-name class-name initargs safe-code-p)
142 (unless (fboundp function-name)
143 (make-ctor function-name class-name initargs safe-code-p)))
145 ;;; Keep this a separate function for testing.
146 (defun make-ctor (function-name class-name initargs safe-p)
147 (without-package-locks ; for (setf symbol-function)
148 (let ((ctor (%make-ctor function-name class-name nil initargs safe-p)))
149 (push ctor *all-ctors*)
150 (setf (fdefinition function-name) ctor)
151 (install-initial-constructor ctor :force-p t)
155 ;;; ***********************************************
156 ;;; Compile-Time Expansion of MAKE-INSTANCE *******
157 ;;; ***********************************************
159 (define-compiler-macro make-instance (&whole form &rest args &environment env)
160 (declare (ignore args))
161 (or (make-instance->constructor-call form (safe-code-p env))
164 (defun make-instance->constructor-call (form safe-code-p)
165 (destructuring-bind (fn class-name &rest args) form
166 (declare (ignore fn))
168 ;; Return the name of parameter number I of a constructor
171 (let ((ps #(.p0. .p1. .p2. .p3. .p4. .p5.)))
172 (if (array-in-bounds-p ps i)
174 (format-symbol *pcl-package* ".P~D." i))))
175 ;; Check if CLASS-NAME is a constant symbol. Give up if
178 (unless (and class-name (constant-symbol-p class-name))
179 (return-from make-instance->constructor-call nil)))
180 ;; Check if ARGS are suitable for an optimized constructor.
181 ;; Return NIL from the outer function if not.
183 (loop for (key . more) on args by #'cddr do
184 (when (or (null more)
185 (not (constant-symbol-p key))
186 (eq :allow-other-keys (constant-form-value key)))
187 (return-from make-instance->constructor-call nil)))))
190 ;; Collect a plist of initargs and constant values/parameter names
191 ;; in INITARGS. Collect non-constant initialization forms in
193 (multiple-value-bind (initargs value-forms)
194 (loop for (key value) on args by #'cddr and i from 0
195 collect (constant-form-value key) into initargs
197 collect value into initargs
199 collect (parameter-name i) into initargs
200 and collect value into value-forms
202 (return (values initargs value-forms)))
203 (let* ((class-name (constant-form-value class-name))
204 (function-name (make-ctor-function-name class-name initargs
206 ;; Prevent compiler warnings for calling the ctor.
207 (proclaim-as-fun-name function-name)
208 (note-name-defined function-name :function)
209 (when (eq (info :function :where-from function-name) :assumed)
210 (setf (info :function :where-from function-name) :defined)
211 (when (info :function :assumed-type function-name)
212 (setf (info :function :assumed-type function-name) nil)))
213 ;; Return code constructing a ctor at load time, which, when
214 ;; called, will set its funcallable instance function to an
215 ;; optimized constructor function.
217 (declare (disable-package-locks ,function-name))
218 (let ((.x. (load-time-value
219 (ensure-ctor ',function-name ',class-name ',initargs
221 (declare (ignore .x.))
222 ;; ??? check if this is worth it.
224 (ftype (or (function ,(make-list (length value-forms)
227 (function (&rest t) t))
229 (funcall (function ,function-name) ,@value-forms))))))))
232 ;;; **************************************************
233 ;;; Load-Time Constructor Function Generation *******
234 ;;; **************************************************
236 ;;; The system-supplied primary INITIALIZE-INSTANCE and
237 ;;; SHARED-INITIALIZE methods. One cannot initialize these variables
238 ;;; to the right values here because said functions don't exist yet
239 ;;; when this file is first loaded.
240 (defvar *the-system-ii-method* nil)
241 (defvar *the-system-si-method* nil)
243 (defun install-optimized-constructor (ctor)
244 (let ((class (find-class (ctor-class-name ctor))))
245 (unless (class-finalized-p class)
246 (finalize-inheritance class))
247 (setf (ctor-class ctor) class)
248 (pushnew ctor (plist-value class 'ctors))
249 (setf (funcallable-instance-fun ctor)
250 (multiple-value-bind (form locations names)
251 (constructor-function-form ctor)
252 (apply (compile nil `(lambda ,names ,form)) locations)))))
254 (defun constructor-function-form (ctor)
255 (let* ((class (ctor-class ctor))
256 (proto (class-prototype class))
257 (make-instance-methods
258 (compute-applicable-methods #'make-instance (list class)))
259 (allocate-instance-methods
260 (compute-applicable-methods #'allocate-instance (list class)))
261 ;; I stared at this in confusion for a while, thinking
262 ;; carefully about the possibility of the class prototype not
263 ;; being of sufficient discrimiating power, given the
264 ;; possibility of EQL-specialized methods on
265 ;; INITIALIZE-INSTANCE or SHARED-INITIALIZE. However, given
266 ;; that this is a constructor optimization, the user doesn't
267 ;; yet have the instance to create a method with such an EQL
270 ;; There remains the (theoretical) possibility of someone
271 ;; coming along with code of the form
273 ;; (defmethod initialize-instance :before ((o foo) ...)
274 ;; (eval `(defmethod shared-initialize :before ((o foo) ...) ...)))
276 ;; but probably we can afford not to worry about this too
277 ;; much for now. -- CSR, 2004-07-12
279 (compute-applicable-methods #'initialize-instance (list proto)))
281 (compute-applicable-methods #'shared-initialize (list proto t)))
282 (setf-svuc-slots-methods
283 (loop for slot in (class-slots class)
284 collect (compute-applicable-methods
285 #'(setf slot-value-using-class)
286 (list nil class proto slot))))
288 (loop for slot in (class-slots class)
289 collect (compute-applicable-methods
290 #'slot-boundp-using-class
291 (list class proto slot)))))
292 ;; Cannot initialize these variables earlier because the generic
293 ;; functions don't exist when PCL is built.
294 (when (null *the-system-si-method*)
295 (setq *the-system-si-method*
296 (find-method #'shared-initialize
297 () (list *the-class-slot-object* *the-class-t*)))
298 (setq *the-system-ii-method*
299 (find-method #'initialize-instance
300 () (list *the-class-slot-object*))))
301 ;; Note that when there are user-defined applicable methods on
302 ;; MAKE-INSTANCE and/or ALLOCATE-INSTANCE, these will show up
303 ;; together with the system-defined ones in what
304 ;; COMPUTE-APPLICABLE-METHODS returns.
305 (if (and (not (structure-class-p class))
306 (not (condition-class-p class))
307 (null (cdr make-instance-methods))
308 (null (cdr allocate-instance-methods))
310 (member (slot-definition-allocation x)
311 '(:instance :class)))
313 (null (check-initargs-1
316 (ctor-default-initkeys
317 (ctor-initargs ctor) (class-default-initargs class))
318 (plist-keys (ctor-initargs ctor)))
319 (append ii-methods si-methods) nil nil))
320 (not (around-or-nonstandard-primary-method-p
321 ii-methods *the-system-ii-method*))
322 (not (around-or-nonstandard-primary-method-p
323 si-methods *the-system-si-method*))
324 ;; the instance structure protocol goes through
325 ;; slot-value(-using-class) and friends (actually just
326 ;; (SETF SLOT-VALUE-USING-CLASS) and
327 ;; SLOT-BOUNDP-USING-CLASS), so if there are non-standard
328 ;; applicable methods we can't shortcircuit them.
329 (every (lambda (x) (= (length x) 1)) setf-svuc-slots-methods)
330 (every (lambda (x) (= (length x) 1)) sbuc-slots-methods))
331 (optimizing-generator ctor ii-methods si-methods)
332 (fallback-generator ctor ii-methods si-methods))))
334 (defun around-or-nonstandard-primary-method-p
335 (methods &optional standard-method)
336 (loop with primary-checked-p = nil
337 for method in methods
338 as qualifiers = (method-qualifiers method)
339 when (or (eq :around (car qualifiers))
340 (and (null qualifiers)
341 (not primary-checked-p)
342 (not (null standard-method))
343 (not (eq standard-method method))))
345 when (null qualifiers) do
346 (setq primary-checked-p t)))
348 (defun fallback-generator (ctor ii-methods si-methods)
349 (declare (ignore ii-methods si-methods))
350 `(lambda ,(make-ctor-parameter-list ctor)
351 ;; The CTOR MAKE-INSTANCE optimization only kicks in when the
352 ;; first argument to MAKE-INSTANCE is a constant symbol: by
353 ;; calling it with a class, as here, we inhibit the optimization,
354 ;; so removing the possibility of endless recursion. -- CSR,
356 (make-instance ,(ctor-class ctor)
357 ,@(quote-plist-keys (ctor-initargs ctor)))))
359 (defun optimizing-generator (ctor ii-methods si-methods)
360 (multiple-value-bind (locations names body before-method-p)
361 (fake-initialization-emf ctor ii-methods si-methods)
363 `(lambda ,(make-ctor-parameter-list ctor)
364 (declare #.*optimize-speed*)
365 ,(wrap-in-allocate-forms ctor body before-method-p))
369 ;;; Return a form wrapped around BODY that allocates an instance
370 ;;; constructed by CTOR. BEFORE-METHOD-P set means we have to run
371 ;;; before-methods, in which case we initialize instance slots to
372 ;;; +SLOT-UNBOUND+. The resulting form binds the local variables
373 ;;; .INSTANCE. to the instance, and .SLOTS. to the instance's slot
374 ;;; vector around BODY.
375 (defun wrap-in-allocate-forms (ctor body before-method-p)
376 (let* ((class (ctor-class ctor))
377 (wrapper (class-wrapper class))
378 (allocation-function (raw-instance-allocator class))
379 (slots-fetcher (slots-fetcher class)))
380 (if (eq allocation-function 'allocate-standard-instance)
381 `(let ((.instance. (%make-standard-instance nil
382 (get-instance-hash-code)))
384 ,(layout-length wrapper)
385 ,@(when before-method-p
386 '(:initial-element +slot-unbound+)))))
387 (setf (std-instance-wrapper .instance.) ,wrapper)
388 (setf (std-instance-slots .instance.) .slots.)
391 `(let* ((.instance. (,allocation-function ,wrapper))
392 (.slots. (,slots-fetcher .instance.)))
393 (declare (ignorable .slots.))
397 ;;; Return a form for invoking METHOD with arguments from ARGS. As
398 ;;; can be seen in METHOD-FUNCTION-FROM-FAST-FUNCTION, method
399 ;;; functions look like (LAMBDA (ARGS NEXT-METHODS) ...). We could
400 ;;; call fast method functions directly here, but benchmarks show that
401 ;;; there's no speed to gain, so lets avoid the hair here.
402 (defmacro invoke-method (method args)
403 `(funcall ,(method-function method) ,args ()))
405 ;;; Return a form that is sort of an effective method comprising all
406 ;;; calls to INITIALIZE-INSTANCE and SHARED-INITIALIZE that would
407 ;;; normally have taken place when calling MAKE-INSTANCE.
408 (defun fake-initialization-emf (ctor ii-methods si-methods)
409 (multiple-value-bind (ii-around ii-before ii-primary ii-after)
410 (standard-sort-methods ii-methods)
411 (declare (ignore ii-primary))
412 (multiple-value-bind (si-around si-before si-primary si-after)
413 (standard-sort-methods si-methods)
414 (declare (ignore si-primary))
415 (aver (and (null ii-around) (null si-around)))
416 (let ((initargs (ctor-initargs ctor)))
417 (multiple-value-bind (locations names bindings vars defaulting-initargs body)
418 (slot-init-forms ctor (or ii-before si-before))
423 (declare (ignorable ,@vars))
424 (let (,@(when (or ii-before ii-after)
426 (list .instance. ,@(quote-plist-keys initargs) ,@defaulting-initargs))))
427 ,@(when (or si-before si-after)
429 (list .instance. t ,@(quote-plist-keys initargs) ,@defaulting-initargs)))))
430 ,@(loop for method in ii-before
431 collect `(invoke-method ,method .ii-args.))
432 ,@(loop for method in si-before
433 collect `(invoke-method ,method .si-args.))
435 ,@(loop for method in si-after
436 collect `(invoke-method ,method .si-args.))
437 ,@(loop for method in ii-after
438 collect `(invoke-method ,method .ii-args.))))
439 (or ii-before si-before)))))))
441 ;;; Return four values from APPLICABLE-METHODS: around methods, before
442 ;;; methods, the applicable primary method, and applicable after
443 ;;; methods. Before and after methods are sorted in the order they
445 (defun standard-sort-methods (applicable-methods)
446 (loop for method in applicable-methods
447 as qualifiers = (method-qualifiers method)
449 collect method into primary
450 else if (eq :around (car qualifiers))
451 collect method into around
452 else if (eq :after (car qualifiers))
453 collect method into after
454 else if (eq :before (car qualifiers))
455 collect method into before
457 (return (values around before (first primary) (reverse after)))))
459 (defmacro with-type-checked ((type safe-p) &body body)
461 ;; To handle FUNCTION types reasonable, we use SAFETY 3 and
462 ;; THE instead of e.g. CHECK-TYPE.
464 (declare (optimize (safety 3)))
465 (the ,type (progn ,@body)))
468 ;;; Return as multiple values bindings for default initialization
469 ;;; arguments, variable names, defaulting initargs and a body for
470 ;;; initializing instance and class slots of an object costructed by
471 ;;; CTOR. The variable .SLOTS. is assumed to bound to the instance's
472 ;;; slot vector. BEFORE-METHOD-P T means before-methods will be
473 ;;; called, which means that 1) other code will initialize instance
474 ;;; slots to +SLOT-UNBOUND+ before the before-methods are run, and
475 ;;; that we have to check if these before-methods have set slots.
476 (defun slot-init-forms (ctor before-method-p)
477 (let* ((class (ctor-class ctor))
478 (initargs (ctor-initargs ctor))
479 (initkeys (plist-keys initargs))
480 (safe-p (ctor-safe-p ctor))
482 (make-array (layout-length (class-wrapper class))
483 :initial-element nil))
486 (defaulting-initargs ())
487 (default-initargs (class-default-initargs class))
489 (compute-initarg-locations
490 class (append initkeys (mapcar #'car default-initargs)))))
491 (labels ((initarg-locations (initarg)
492 (cdr (assoc initarg initarg-locations :test #'eq)))
493 (initializedp (location)
496 (assoc location class-inits :test #'eq))
498 (not (null (aref slot-vector location))))
499 (t (bug "Weird location in ~S" 'slot-init-forms))))
500 (class-init (location kind val type)
501 (aver (consp location))
502 (unless (initializedp location)
503 (push (list location kind val type) class-inits)))
504 (instance-init (location kind val type)
505 (aver (integerp location))
506 (unless (initializedp location)
507 (setf (aref slot-vector location) (list kind val type))))
508 (default-init-var-name (i)
509 (let ((ps #(.d0. .d1. .d2. .d3. .d4. .d5.)))
510 (if (array-in-bounds-p ps i)
512 (format-symbol *pcl-package* ".D~D." i))))
513 (location-var-name (i)
514 (let ((ls #(.l0. .l1. .l2. .l3. .l4. .l5.)))
515 (if (array-in-bounds-p ls i)
517 (format-symbol *pcl-package* ".L~D." i)))))
518 ;; Loop over supplied initargs and values and record which
519 ;; instance and class slots they initialize.
520 (loop for (key value) on initargs by #'cddr
521 as kind = (if (constantp value) 'constant 'param)
522 as locations = (initarg-locations key)
523 do (loop for (location . type) in locations
524 do (if (consp location)
525 (class-init location kind value type)
526 (instance-init location kind value type))))
527 ;; Loop over default initargs of the class, recording
528 ;; initializations of slots that have not been initialized
529 ;; above. Default initargs which are not in the supplied
530 ;; initargs are treated as if they were appended to supplied
531 ;; initargs, that is, their values must be evaluated even
532 ;; if not actually used for initializing a slot.
533 (loop for (key initform initfn) in default-initargs and i from 0
534 unless (member key initkeys :test #'eq) do
535 (let* ((kind (if (constantp initform) 'constant 'var))
536 (init (if (eq kind 'var) initfn initform)))
539 (push key defaulting-initargs)
540 (push initform defaulting-initargs))
542 (push key defaulting-initargs)
543 (push (default-init-var-name i) defaulting-initargs)))
545 (let ((init-var (default-init-var-name i)))
547 (push (cons init-var initfn) default-inits)))
548 (loop for (location . type) in (initarg-locations key)
549 do (if (consp location)
550 (class-init location kind init type)
551 (instance-init location kind init type)))))
552 ;; Loop over all slots of the class, filling in the rest from
554 (loop for slotd in (class-slots class)
555 as location = (slot-definition-location slotd)
556 as type = (slot-definition-type slotd)
557 as allocation = (slot-definition-allocation slotd)
558 as initfn = (slot-definition-initfunction slotd)
559 as initform = (slot-definition-initform slotd) do
560 (unless (or (eq allocation :class)
562 (initializedp location))
563 (if (constantp initform)
564 (instance-init location 'initform initform type)
565 (instance-init location 'initform/initfn initfn type))))
566 ;; Generate the forms for initializing instance and class slots.
567 (let ((instance-init-forms
568 (loop for slot-entry across slot-vector and i from 0
569 as (kind value type) = slot-entry collect
572 (unless before-method-p
573 `(setf (clos-slots-ref .slots. ,i) +slot-unbound+)))
575 `(setf (clos-slots-ref .slots. ,i)
576 (with-type-checked (,type ,safe-p)
579 `(setf (clos-slots-ref .slots. ,i)
580 (with-type-checked (,type ,safe-p)
584 `(when (eq (clos-slots-ref .slots. ,i)
586 (setf (clos-slots-ref .slots. ,i)
587 (with-type-checked (,type ,safe-p)
589 `(setf (clos-slots-ref .slots. ,i)
590 (with-type-checked (,type ,safe-p)
594 `(when (eq (clos-slots-ref .slots. ,i)
596 (setf (clos-slots-ref .slots. ,i)
597 (with-type-checked (,type ,safe-p)
598 ',(constant-form-value value))))
599 `(setf (clos-slots-ref .slots. ,i)
600 (with-type-checked (,type ,safe-p)
601 ',(constant-form-value value)))))
603 `(setf (clos-slots-ref .slots. ,i)
604 (with-type-checked (,type ,safe-p)
605 ',(constant-form-value value))))))))
606 ;; we are not allowed to modify QUOTEd locations, so we can't
607 ;; generate code like (setf (cdr ',location) arg). Instead,
608 ;; we have to do (setf (cdr .L0.) arg) and arrange for .L0. to
609 ;; be bound to the location.
610 (multiple-value-bind (names locations class-init-forms)
611 (loop for (location kind value type) in class-inits
613 for name = (location-var-name i)
614 collect name into names
615 collect location into locations
616 collect `(setf (cdr ,name)
617 (with-type-checked (,type ,safe-p)
619 (constant `',(constant-form-value value))
620 ((param var) `,value)
621 (initfn `(funcall ,value)))))
622 into class-init-forms
623 finally (return (values names locations class-init-forms)))
624 (multiple-value-bind (vars bindings)
625 (loop for (var . initfn) in (nreverse default-inits)
626 collect var into vars
627 collect `(,var (funcall ,initfn)) into bindings
628 finally (return (values vars bindings)))
629 (values locations names
631 (nreverse defaulting-initargs)
632 `(,@(delete nil instance-init-forms)
633 ,@class-init-forms))))))))
635 ;;; Return an alist of lists (KEY (LOCATION . TYPE-SPECIFIER) ...)
636 ;;; telling, for each key in INITKEYS, which locations the initarg
637 ;;; initializes and the associated type with the location. CLASS is
638 ;;; the class of the instance being initialized.
639 (defun compute-initarg-locations (class initkeys)
640 (loop with slots = (class-slots class)
641 for key in initkeys collect
642 (loop for slot in slots
643 if (memq key (slot-definition-initargs slot))
644 collect (cons (slot-definition-location slot)
645 (slot-definition-type slot))
648 collect slot into remaining-slots
650 (setq slots remaining-slots)
651 (return (cons key locations)))))
654 ;;; *******************************
655 ;;; External Entry Points ********
656 ;;; *******************************
658 (defun update-ctors (reason &key class name generic-function method)
659 (labels ((reset (class &optional ri-cache-p (ctorsp t))
661 (dolist (ctor (plist-value class 'ctors))
662 (install-initial-constructor ctor)))
664 (setf (plist-value class 'ri-initargs) ()))
665 (dolist (subclass (class-direct-subclasses class))
666 (reset subclass ri-cache-p ctorsp))))
668 ;; CLASS must have been specified.
669 (finalize-inheritance
671 ;; NAME must have been specified.
673 (loop for ctor in *all-ctors*
674 when (eq (ctor-class-name ctor) name) do
675 (when (ctor-class ctor)
676 (reset (ctor-class ctor)))
678 ;; GENERIC-FUNCTION and METHOD must have been specified.
679 ((add-method remove-method)
680 (flet ((class-of-1st-method-param (method)
681 (type-class (first (method-specializers method)))))
682 (case (generic-function-name generic-function)
683 ((make-instance allocate-instance
684 initialize-instance shared-initialize)
685 (reset (class-of-1st-method-param method) t t))
686 ((reinitialize-instance)
687 (reset (class-of-1st-method-param method) t nil))
688 (t (when (or (eq (generic-function-name generic-function)
689 'slot-boundp-using-class)
690 (equal (generic-function-name generic-function)
691 '(setf slot-value-using-class)))
692 ;; this looks awfully expensive, but given that one
693 ;; can specialize on the SLOTD argument, nothing is
694 ;; safe. -- CSR, 2004-07-12
695 (reset (find-class 'standard-object))))))))))
697 (defun precompile-ctors ()
698 (dolist (ctor *all-ctors*)
699 (when (null (ctor-class ctor))
700 (let ((class (find-class (ctor-class-name ctor) nil)))
701 (when (and class (class-finalized-p class))
702 (install-optimized-constructor ctor))))))
704 (defun check-ri-initargs (instance initargs)
705 (let* ((class (class-of instance))
706 (keys (plist-keys initargs))
707 (cached (assoc keys (plist-value class 'ri-initargs)
713 ;; FIXME: give CHECK-INITARGS-1 and friends a
714 ;; more mnemonic name and (possibly) a nicer,
715 ;; more orthogonal interface.
718 (list (list* 'reinitialize-instance instance initargs)
719 (list* 'shared-initialize instance nil initargs))
721 (setf (plist-value class 'ri-initargs)
722 (acons keys invalid cached))
725 (error 'initarg-error :class class :initargs invalid-keys))))