1.0.19.33: Improved interrupt handling on darwin/x86[-64]
[sbcl/eslaughter.git] / src / pcl / ctor.lisp
blob0aae966df2eadadaef4d633ca249c1313277b890
1 ;;;; This file contains the optimization machinery for make-instance.
3 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; more information.
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.
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.
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.
59 (in-package "SB-PCL")
61 ;;; ******************
62 ;;; Utilities *******
63 ;;; ******************
65 (defun quote-plist-keys (plist)
66 (loop for (key . more) on plist by #'cddr
67 if (null more) do
68 (error "Not a property list: ~S" plist)
69 else
70 collect `(quote ,key)
71 and collect (car more)))
73 (defun plist-keys (plist &key test)
74 (loop for (key . more) on plist by #'cddr
75 if (null more) do
76 (error "Not a property list: ~S" plist)
77 else if (or (null test) (funcall test key))
78 collect key))
80 (defun plist-values (plist &key test)
81 (loop for (key . more) on plist by #'cddr
82 if (null more) do
83 (error "Not a property list: ~S" plist)
84 else if (or (null test) (funcall test (car more)))
85 collect (car more)))
87 (defun constant-symbol-p (form)
88 (and (constantp 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.)
98 collect key))
100 ;;; *****************
101 ;;; CTORS *********
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)
133 (apply ctor args)))
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)
152 ctor)))
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))
162 form))
164 (defun make-instance->constructor-call (form safe-code-p)
165 (destructuring-bind (fn class-name &rest args) form
166 (declare (ignore fn))
167 (flet (;;
168 ;; Return the name of parameter number I of a constructor
169 ;; function.
170 (parameter-name (i)
171 (let ((ps #(.p0. .p1. .p2. .p3. .p4. .p5.)))
172 (if (array-in-bounds-p ps i)
173 (aref ps i)
174 (format-symbol *pcl-package* ".P~D." i))))
175 ;; Check if CLASS-NAME is a constant symbol. Give up if
176 ;; not.
177 (check-class ()
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.
182 (check-args ()
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)))))
188 (check-class)
189 (check-args)
190 ;; Collect a plist of initargs and constant values/parameter names
191 ;; in INITARGS. Collect non-constant initialization forms in
192 ;; VALUE-FORMS.
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
196 if (constantp value)
197 collect value into initargs
198 else
199 collect (parameter-name i) into initargs
200 and collect value into value-forms
201 finally
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
205 safe-code-p)))
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.
216 `(locally
217 (declare (disable-package-locks ,function-name))
218 (let ((.x. (load-time-value
219 (ensure-ctor ',function-name ',class-name ',initargs
220 ',safe-code-p))))
221 (declare (ignore .x.))
222 ;; ??? check if this is worth it.
223 (declare
224 (ftype (or (function ,(make-list (length value-forms)
225 :initial-element t)
227 (function (&rest t) t))
228 ,function-name))
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 ;; We can have a class with an invalid layout here. Such a class
248 ;; cannot have a LAYOUT-INVALID of (:FLUSH ...) or (:OBSOLETE
249 ;; ...), because part of the deal is that those only happen from
250 ;; FORCE-CACHE-FLUSHES, which create a new valid wrapper for the
251 ;; class. An invalid layout of T needs to be flushed, however.
252 (when (eq (layout-invalid (class-wrapper class)) t)
253 (force-cache-flushes class))
254 (setf (ctor-class ctor) class)
255 (pushnew ctor (plist-value class 'ctors) :test #'eq)
256 (setf (funcallable-instance-fun ctor)
257 (multiple-value-bind (form locations names)
258 (constructor-function-form ctor)
259 (apply (compile nil `(lambda ,names ,form)) locations)))))
261 (defun constructor-function-form (ctor)
262 (let* ((class (ctor-class ctor))
263 (proto (class-prototype class))
264 (make-instance-methods
265 (compute-applicable-methods #'make-instance (list class)))
266 (allocate-instance-methods
267 (compute-applicable-methods #'allocate-instance (list class)))
268 ;; I stared at this in confusion for a while, thinking
269 ;; carefully about the possibility of the class prototype not
270 ;; being of sufficient discrimiating power, given the
271 ;; possibility of EQL-specialized methods on
272 ;; INITIALIZE-INSTANCE or SHARED-INITIALIZE. However, given
273 ;; that this is a constructor optimization, the user doesn't
274 ;; yet have the instance to create a method with such an EQL
275 ;; specializer.
277 ;; There remains the (theoretical) possibility of someone
278 ;; coming along with code of the form
280 ;; (defmethod initialize-instance :before ((o foo) ...)
281 ;; (eval `(defmethod shared-initialize :before ((o foo) ...) ...)))
283 ;; but probably we can afford not to worry about this too
284 ;; much for now. -- CSR, 2004-07-12
285 (ii-methods
286 (compute-applicable-methods #'initialize-instance (list proto)))
287 (si-methods
288 (compute-applicable-methods #'shared-initialize (list proto t)))
289 (setf-svuc-slots-methods
290 (loop for slot in (class-slots class)
291 collect (compute-applicable-methods
292 #'(setf slot-value-using-class)
293 (list nil class proto slot))))
294 (sbuc-slots-methods
295 (loop for slot in (class-slots class)
296 collect (compute-applicable-methods
297 #'slot-boundp-using-class
298 (list class proto slot)))))
299 ;; Cannot initialize these variables earlier because the generic
300 ;; functions don't exist when PCL is built.
301 (when (null *the-system-si-method*)
302 (setq *the-system-si-method*
303 (find-method #'shared-initialize
304 () (list *the-class-slot-object* *the-class-t*)))
305 (setq *the-system-ii-method*
306 (find-method #'initialize-instance
307 () (list *the-class-slot-object*))))
308 ;; Note that when there are user-defined applicable methods on
309 ;; MAKE-INSTANCE and/or ALLOCATE-INSTANCE, these will show up
310 ;; together with the system-defined ones in what
311 ;; COMPUTE-APPLICABLE-METHODS returns.
312 (if (and (not (structure-class-p class))
313 (not (condition-class-p class))
314 (null (cdr make-instance-methods))
315 (null (cdr allocate-instance-methods))
316 (every (lambda (x)
317 (member (slot-definition-allocation x)
318 '(:instance :class)))
319 (class-slots class))
320 (null (check-initargs-1
321 class
322 (append
323 (ctor-default-initkeys
324 (ctor-initargs ctor) (class-default-initargs class))
325 (plist-keys (ctor-initargs ctor)))
326 (append ii-methods si-methods) nil nil))
327 (not (around-or-nonstandard-primary-method-p
328 ii-methods *the-system-ii-method*))
329 (not (around-or-nonstandard-primary-method-p
330 si-methods *the-system-si-method*))
331 ;; the instance structure protocol goes through
332 ;; slot-value(-using-class) and friends (actually just
333 ;; (SETF SLOT-VALUE-USING-CLASS) and
334 ;; SLOT-BOUNDP-USING-CLASS), so if there are non-standard
335 ;; applicable methods we can't shortcircuit them.
336 (every (lambda (x) (= (length x) 1)) setf-svuc-slots-methods)
337 (every (lambda (x) (= (length x) 1)) sbuc-slots-methods))
338 (optimizing-generator ctor ii-methods si-methods)
339 (fallback-generator ctor ii-methods si-methods))))
341 (defun around-or-nonstandard-primary-method-p
342 (methods &optional standard-method)
343 (loop with primary-checked-p = nil
344 for method in methods
345 as qualifiers = (if (consp method)
346 (early-method-qualifiers method)
347 (safe-method-qualifiers method))
348 when (or (eq :around (car qualifiers))
349 (and (null qualifiers)
350 (not primary-checked-p)
351 (not (null standard-method))
352 (not (eq standard-method method))))
353 return t
354 when (null qualifiers) do
355 (setq primary-checked-p t)))
357 (defun fallback-generator (ctor ii-methods si-methods)
358 (declare (ignore ii-methods si-methods))
359 `(lambda ,(make-ctor-parameter-list ctor)
360 ;; The CTOR MAKE-INSTANCE optimization only kicks in when the
361 ;; first argument to MAKE-INSTANCE is a constant symbol: by
362 ;; calling it with a class, as here, we inhibit the optimization,
363 ;; so removing the possibility of endless recursion. -- CSR,
364 ;; 2004-07-12
365 (make-instance ,(ctor-class ctor)
366 ,@(quote-plist-keys (ctor-initargs ctor)))))
368 (defun optimizing-generator (ctor ii-methods si-methods)
369 (multiple-value-bind (locations names body before-method-p)
370 (fake-initialization-emf ctor ii-methods si-methods)
371 (let ((wrapper (class-wrapper (ctor-class ctor))))
372 (values
373 `(lambda ,(make-ctor-parameter-list ctor)
374 (declare #.*optimize-speed*)
375 (block nil
376 (when (layout-invalid ,wrapper)
377 (install-initial-constructor ,ctor)
378 (return (funcall ,ctor ,@(make-ctor-parameter-list ctor))))
379 ,(wrap-in-allocate-forms ctor body before-method-p)))
380 locations
381 names))))
383 ;;; Return a form wrapped around BODY that allocates an instance
384 ;;; constructed by CTOR. BEFORE-METHOD-P set means we have to run
385 ;;; before-methods, in which case we initialize instance slots to
386 ;;; +SLOT-UNBOUND+. The resulting form binds the local variables
387 ;;; .INSTANCE. to the instance, and .SLOTS. to the instance's slot
388 ;;; vector around BODY.
389 (defun wrap-in-allocate-forms (ctor body before-method-p)
390 (let* ((class (ctor-class ctor))
391 (wrapper (class-wrapper class))
392 (allocation-function (raw-instance-allocator class))
393 (slots-fetcher (slots-fetcher class)))
394 (if (eq allocation-function 'allocate-standard-instance)
395 `(let ((.instance. (%make-standard-instance nil
396 (get-instance-hash-code)))
397 (.slots. (make-array
398 ,(layout-length wrapper)
399 ,@(when before-method-p
400 '(:initial-element +slot-unbound+)))))
401 (setf (std-instance-wrapper .instance.) ,wrapper)
402 (setf (std-instance-slots .instance.) .slots.)
403 ,body
404 .instance.)
405 `(let* ((.instance. (,allocation-function ,wrapper))
406 (.slots. (,slots-fetcher .instance.)))
407 (declare (ignorable .slots.))
408 ,body
409 .instance.))))
411 ;;; Return a form for invoking METHOD with arguments from ARGS. As
412 ;;; can be seen in METHOD-FUNCTION-FROM-FAST-FUNCTION, method
413 ;;; functions look like (LAMBDA (ARGS NEXT-METHODS) ...). We could
414 ;;; call fast method functions directly here, but benchmarks show that
415 ;;; there's no speed to gain, so lets avoid the hair here.
416 (defmacro invoke-method (method args)
417 `(funcall ,(method-function method) ,args ()))
419 ;;; Return a form that is sort of an effective method comprising all
420 ;;; calls to INITIALIZE-INSTANCE and SHARED-INITIALIZE that would
421 ;;; normally have taken place when calling MAKE-INSTANCE.
422 (defun fake-initialization-emf (ctor ii-methods si-methods)
423 (multiple-value-bind (ii-around ii-before ii-primary ii-after)
424 (standard-sort-methods ii-methods)
425 (declare (ignore ii-primary))
426 (multiple-value-bind (si-around si-before si-primary si-after)
427 (standard-sort-methods si-methods)
428 (declare (ignore si-primary))
429 (aver (and (null ii-around) (null si-around)))
430 (let ((initargs (ctor-initargs ctor)))
431 (multiple-value-bind (locations names bindings vars defaulting-initargs body)
432 (slot-init-forms ctor (or ii-before si-before))
433 (values
434 locations
435 names
436 `(let ,bindings
437 (declare (ignorable ,@vars))
438 (let (,@(when (or ii-before ii-after)
439 `((.ii-args.
440 (list .instance. ,@(quote-plist-keys initargs) ,@defaulting-initargs))))
441 ,@(when (or si-before si-after)
442 `((.si-args.
443 (list .instance. t ,@(quote-plist-keys initargs) ,@defaulting-initargs)))))
444 ,@(loop for method in ii-before
445 collect `(invoke-method ,method .ii-args.))
446 ,@(loop for method in si-before
447 collect `(invoke-method ,method .si-args.))
448 ,@body
449 ,@(loop for method in si-after
450 collect `(invoke-method ,method .si-args.))
451 ,@(loop for method in ii-after
452 collect `(invoke-method ,method .ii-args.))))
453 (or ii-before si-before)))))))
455 ;;; Return four values from APPLICABLE-METHODS: around methods, before
456 ;;; methods, the applicable primary method, and applicable after
457 ;;; methods. Before and after methods are sorted in the order they
458 ;;; must be called.
459 (defun standard-sort-methods (applicable-methods)
460 (loop for method in applicable-methods
461 as qualifiers = (if (consp method)
462 (early-method-qualifiers method)
463 (safe-method-qualifiers method))
464 if (null qualifiers)
465 collect method into primary
466 else if (eq :around (car qualifiers))
467 collect method into around
468 else if (eq :after (car qualifiers))
469 collect method into after
470 else if (eq :before (car qualifiers))
471 collect method into before
472 finally
473 (return (values around before (first primary) (reverse after)))))
475 (defmacro with-type-checked ((type safe-p) &body body)
476 (if safe-p
477 ;; To handle FUNCTION types reasonable, we use SAFETY 3 and
478 ;; THE instead of e.g. CHECK-TYPE.
479 `(locally
480 (declare (optimize (safety 3)))
481 (the ,type (progn ,@body)))
482 `(progn ,@body)))
484 ;;; Return as multiple values bindings for default initialization
485 ;;; arguments, variable names, defaulting initargs and a body for
486 ;;; initializing instance and class slots of an object costructed by
487 ;;; CTOR. The variable .SLOTS. is assumed to bound to the instance's
488 ;;; slot vector. BEFORE-METHOD-P T means before-methods will be
489 ;;; called, which means that 1) other code will initialize instance
490 ;;; slots to +SLOT-UNBOUND+ before the before-methods are run, and
491 ;;; that we have to check if these before-methods have set slots.
492 (defun slot-init-forms (ctor before-method-p)
493 (let* ((class (ctor-class ctor))
494 (initargs (ctor-initargs ctor))
495 (initkeys (plist-keys initargs))
496 (safe-p (ctor-safe-p ctor))
497 (slot-vector
498 (make-array (layout-length (class-wrapper class))
499 :initial-element nil))
500 (class-inits ())
501 (default-inits ())
502 (defaulting-initargs ())
503 (default-initargs (class-default-initargs class))
504 (initarg-locations
505 (compute-initarg-locations
506 class (append initkeys (mapcar #'car default-initargs)))))
507 (labels ((initarg-locations (initarg)
508 (cdr (assoc initarg initarg-locations :test #'eq)))
509 (initializedp (location)
510 (cond
511 ((consp location)
512 (assoc location class-inits :test #'eq))
513 ((integerp location)
514 (not (null (aref slot-vector location))))
515 (t (bug "Weird location in ~S" 'slot-init-forms))))
516 (class-init (location kind val type)
517 (aver (consp location))
518 (unless (initializedp location)
519 (push (list location kind val type) class-inits)))
520 (instance-init (location kind val type)
521 (aver (integerp location))
522 (unless (initializedp location)
523 (setf (aref slot-vector location) (list kind val type))))
524 (default-init-var-name (i)
525 (let ((ps #(.d0. .d1. .d2. .d3. .d4. .d5.)))
526 (if (array-in-bounds-p ps i)
527 (aref ps i)
528 (format-symbol *pcl-package* ".D~D." i))))
529 (location-var-name (i)
530 (let ((ls #(.l0. .l1. .l2. .l3. .l4. .l5.)))
531 (if (array-in-bounds-p ls i)
532 (aref ls i)
533 (format-symbol *pcl-package* ".L~D." i)))))
534 ;; Loop over supplied initargs and values and record which
535 ;; instance and class slots they initialize.
536 (loop for (key value) on initargs by #'cddr
537 as kind = (if (constantp value) 'constant 'param)
538 as locations = (initarg-locations key)
539 do (loop for (location . type) in locations
540 do (if (consp location)
541 (class-init location kind value type)
542 (instance-init location kind value type))))
543 ;; Loop over default initargs of the class, recording
544 ;; initializations of slots that have not been initialized
545 ;; above. Default initargs which are not in the supplied
546 ;; initargs are treated as if they were appended to supplied
547 ;; initargs, that is, their values must be evaluated even
548 ;; if not actually used for initializing a slot.
549 (loop for (key initform initfn) in default-initargs and i from 0
550 unless (member key initkeys :test #'eq)
551 do (let* ((kind (if (constantp initform) 'constant 'var))
552 (init (if (eq kind 'var) initfn initform)))
553 (ecase kind
554 (constant
555 (push (list 'quote key) defaulting-initargs)
556 (push initform defaulting-initargs))
557 (var
558 (push (list 'quote key) defaulting-initargs)
559 (push (default-init-var-name i) defaulting-initargs)))
560 (when (eq kind 'var)
561 (let ((init-var (default-init-var-name i)))
562 (setq init init-var)
563 (push (cons init-var initfn) default-inits)))
564 (loop for (location . type) in (initarg-locations key)
565 do (if (consp location)
566 (class-init location kind init type)
567 (instance-init location kind init type)))))
568 ;; Loop over all slots of the class, filling in the rest from
569 ;; slot initforms.
570 (loop for slotd in (class-slots class)
571 as location = (slot-definition-location slotd)
572 as type = (slot-definition-type slotd)
573 as allocation = (slot-definition-allocation slotd)
574 as initfn = (slot-definition-initfunction slotd)
575 as initform = (slot-definition-initform slotd) do
576 (unless (or (eq allocation :class)
577 (null initfn)
578 (initializedp location))
579 (if (constantp initform)
580 (instance-init location 'initform initform type)
581 (instance-init location 'initform/initfn initfn type))))
582 ;; Generate the forms for initializing instance and class slots.
583 (let ((instance-init-forms
584 (loop for slot-entry across slot-vector and i from 0
585 as (kind value type) = slot-entry collect
586 (ecase kind
587 ((nil)
588 (unless before-method-p
589 `(setf (clos-slots-ref .slots. ,i) +slot-unbound+)))
590 ((param var)
591 `(setf (clos-slots-ref .slots. ,i)
592 (with-type-checked (,type ,safe-p)
593 ,value)))
594 (initfn
595 `(setf (clos-slots-ref .slots. ,i)
596 (with-type-checked (,type ,safe-p)
597 (funcall ,value))))
598 (initform/initfn
599 (if before-method-p
600 `(when (eq (clos-slots-ref .slots. ,i)
601 +slot-unbound+)
602 (setf (clos-slots-ref .slots. ,i)
603 (with-type-checked (,type ,safe-p)
604 (funcall ,value))))
605 `(setf (clos-slots-ref .slots. ,i)
606 (with-type-checked (,type ,safe-p)
607 (funcall ,value)))))
608 (initform
609 (if before-method-p
610 `(when (eq (clos-slots-ref .slots. ,i)
611 +slot-unbound+)
612 (setf (clos-slots-ref .slots. ,i)
613 (with-type-checked (,type ,safe-p)
614 ',(constant-form-value value))))
615 `(setf (clos-slots-ref .slots. ,i)
616 (with-type-checked (,type ,safe-p)
617 ',(constant-form-value value)))))
618 (constant
619 `(setf (clos-slots-ref .slots. ,i)
620 (with-type-checked (,type ,safe-p)
621 ',(constant-form-value value))))))))
622 ;; we are not allowed to modify QUOTEd locations, so we can't
623 ;; generate code like (setf (cdr ',location) arg). Instead,
624 ;; we have to do (setf (cdr .L0.) arg) and arrange for .L0. to
625 ;; be bound to the location.
626 (multiple-value-bind (names locations class-init-forms)
627 (loop for (location kind value type) in class-inits
628 for i upfrom 0
629 for name = (location-var-name i)
630 collect name into names
631 collect location into locations
632 collect `(setf (cdr ,name)
633 (with-type-checked (,type ,safe-p)
634 ,(case kind
635 (constant `',(constant-form-value value))
636 ((param var) `,value)
637 (initfn `(funcall ,value)))))
638 into class-init-forms
639 finally (return (values names locations class-init-forms)))
640 (multiple-value-bind (vars bindings)
641 (loop for (var . initfn) in (nreverse default-inits)
642 collect var into vars
643 collect `(,var (funcall ,initfn)) into bindings
644 finally (return (values vars bindings)))
645 (values locations names
646 bindings vars
647 (nreverse defaulting-initargs)
648 `(,@(delete nil instance-init-forms)
649 ,@class-init-forms))))))))
651 ;;; Return an alist of lists (KEY (LOCATION . TYPE-SPECIFIER) ...)
652 ;;; telling, for each key in INITKEYS, which locations the initarg
653 ;;; initializes and the associated type with the location. CLASS is
654 ;;; the class of the instance being initialized.
655 (defun compute-initarg-locations (class initkeys)
656 (loop with slots = (class-slots class)
657 for key in initkeys collect
658 (loop for slot in slots
659 if (memq key (slot-definition-initargs slot))
660 collect (cons (slot-definition-location slot)
661 (slot-definition-type slot))
662 into locations
663 else
664 collect slot into remaining-slots
665 finally
666 (setq slots remaining-slots)
667 (return (cons key locations)))))
670 ;;; *******************************
671 ;;; External Entry Points ********
672 ;;; *******************************
674 (defun update-ctors (reason &key class name generic-function method)
675 (labels ((reset (class &optional ri-cache-p (ctorsp t))
676 (when ctorsp
677 (dolist (ctor (plist-value class 'ctors))
678 (install-initial-constructor ctor)))
679 (when ri-cache-p
680 (setf (plist-value class 'ri-initargs) ()))
681 (dolist (subclass (class-direct-subclasses class))
682 (reset subclass ri-cache-p ctorsp))))
683 (ecase reason
684 ;; CLASS must have been specified.
685 (finalize-inheritance
686 (reset class t))
687 ;; NAME must have been specified.
688 (setf-find-class
689 (loop for ctor in *all-ctors*
690 when (eq (ctor-class-name ctor) name) do
691 (when (ctor-class ctor)
692 (reset (ctor-class ctor)))
693 (loop-finish)))
694 ;; GENERIC-FUNCTION and METHOD must have been specified.
695 ((add-method remove-method)
696 (flet ((class-of-1st-method-param (method)
697 (type-class (first (method-specializers method)))))
698 (case (generic-function-name generic-function)
699 ((make-instance allocate-instance
700 initialize-instance shared-initialize)
701 (reset (class-of-1st-method-param method) t t))
702 ((reinitialize-instance)
703 (reset (class-of-1st-method-param method) t nil))
704 (t (when (or (eq (generic-function-name generic-function)
705 'slot-boundp-using-class)
706 (equal (generic-function-name generic-function)
707 '(setf slot-value-using-class)))
708 ;; this looks awfully expensive, but given that one
709 ;; can specialize on the SLOTD argument, nothing is
710 ;; safe. -- CSR, 2004-07-12
711 (reset (find-class 'standard-object))))))))))
713 (defun precompile-ctors ()
714 (dolist (ctor *all-ctors*)
715 (when (null (ctor-class ctor))
716 (let ((class (find-class (ctor-class-name ctor) nil)))
717 (when (and class (class-finalized-p class))
718 (install-optimized-constructor ctor))))))
720 (defun check-ri-initargs (instance initargs)
721 (let* ((class (class-of instance))
722 (keys (plist-keys initargs))
723 (cached (assoc keys (plist-value class 'ri-initargs)
724 :test #'equal))
725 (invalid-keys
726 (if (consp cached)
727 (cdr cached)
728 (let ((invalid
729 ;; FIXME: give CHECK-INITARGS-1 and friends a
730 ;; more mnemonic name and (possibly) a nicer,
731 ;; more orthogonal interface.
732 (check-initargs-1
733 class initargs
734 (list (list* 'reinitialize-instance instance initargs)
735 (list* 'shared-initialize instance nil initargs))
736 t nil)))
737 (setf (plist-value class 'ri-initargs)
738 (acons keys invalid cached))
739 invalid))))
740 (when invalid-keys
741 (error 'initarg-error :class class :initargs invalid-keys))))
743 ;;; end of ctor.lisp