Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

[ea1220]: src / pcl / ctor.lisp Maximize Restore History

Download this file

ctor.lisp    708 lines (664 with data), 28.7 kB

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