1 ;;; eieio-core.el --- Core implementation for eieio -*- lexical-binding:t -*-
3 ;; Copyright (C) 1995-1996, 1998-2017 Free Software Foundation, Inc.
5 ;; Author: Eric M. Ludlam <zappo@gnu.org>
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;; The "core" part of EIEIO is the implementation for the object
27 ;; system (such as eieio-defclass, or eieio-defmethod) but not the
28 ;; base classes for the object system, which are defined in EIEIO.
30 ;; See the commentary for eieio.el for more about EIEIO itself.
36 (require 'eieio-loaddefs
)
39 ;; A few functions that are better in the official EIEIO src, but
40 ;; used from the core.
41 (declare-function slot-unbound
"eieio")
42 (declare-function slot-missing
"eieio")
43 (declare-function child-of-class-p
"eieio")
44 (declare-function same-class-p
"eieio")
45 (declare-function object-of-class-p
"eieio")
49 ;; Variable declarations.
51 (defvar eieio-hook nil
52 "This hook is executed, then cleared each time `defclass' is called.")
54 (defvar eieio-error-unsupported-class-tags nil
55 "Non-nil to throw an error if an encountered tag is unsupported.
56 This may prevent classes from CLOS applications from being used with EIEIO
57 since EIEIO does not support all CLOS tags.")
59 (defvar eieio-skip-typecheck nil
60 "If non-nil, skip all slot typechecking.
61 Set this to t permanently if a program is functioning well to get a
62 small speed increase. This variable is also used internally to handle
63 default setting for optimization purposes.")
65 (defvar eieio-optimize-primary-methods-flag t
66 "Non-nil means to optimize the method dispatch on primary methods.")
68 (defvar eieio-backward-compatibility t
69 "If nil, drop support for some behaviors of older versions of EIEIO.
70 Currently under control of this var:
71 - Define every class as a var whose value is the class symbol.
72 - Define <class>-child-p and <class>-list-p predicates.
73 - Allow object names in constructors.")
75 (defconst eieio-unbound
76 (if (and (boundp 'eieio-unbound
) (symbolp eieio-unbound
))
78 (make-symbol "unbound"))
79 "Uninterned symbol representing an unbound slot in an object.")
81 ;; This is a bootstrap for eieio-default-superclass so it has a value
82 ;; while it is being built itself.
83 (defvar eieio-default-superclass nil
)
86 ;; Arrange for field access not to bother checking if the access is indeed
87 ;; made to an eieio--class object.
88 (cl-declaim (optimize (safety 0)))
90 (cl-defstruct (eieio--class
92 (:constructor eieio--class-make
(name))
96 initarg-tuples
;; initarg tuples list
97 (class-slots nil
:type eieio--slot
)
98 class-allocation-values
;; class allocated value vector
99 default-object-cache
;; what a newly created object would look like.
100 ; This will speed up instantiation time as
101 ; only a `copy-sequence' will be needed, instead of
102 ; looping over all the values and setting them from
104 options
;; storage location of tagged class option
105 ; Stored outright without modifications or stripping
107 ;; Set it back to the default value.
108 (cl-declaim (optimize (safety 1))))
111 (cl-defstruct (eieio--object
112 (:type vector
) ;We manage our own tagging system.
115 ;; `class-tag' holds a symbol, which is not the class name, but is instead
116 ;; properly prefixed as an internal EIEIO thingy and which holds the class
117 ;; object/struct in its `symbol-value' slot.
121 (defconst eieio--object-num-slots
122 (length (cl-struct-slot-info 'eieio--object
))))
124 (defsubst eieio--object-class
(obj)
125 (symbol-value (eieio--object-class-tag obj
)))
128 ;;; Important macros used internally in eieio.
130 (require 'cl-macs
) ;For cl--find-class.
132 (defsubst eieio--class-object
(class)
133 "Return the class object."
135 ;; Keep the symbol if class-v is nil, for better error messages.
136 (or (cl--find-class class
) class
)
140 "Return non-nil if X is a valid class vector.
141 X can also be is a symbol."
142 (eieio--class-p (if (symbolp x
) (cl--find-class x
) x
)))
144 (defun eieio--class-print-name (class)
145 "Return a printed representation of CLASS."
146 (format "#<class %s>" (eieio-class-name class
)))
148 (defun eieio-class-name (class)
149 "Return a Lisp like symbol name for CLASS."
150 (setq class
(eieio--class-object class
))
151 (cl-check-type class eieio--class
)
152 (eieio--class-name class
))
153 (define-obsolete-function-alias 'class-name
#'eieio-class-name
"24.4")
155 (defalias 'eieio--class-constructor
#'identity
156 "Return the symbol representing the constructor of CLASS.")
158 (defmacro eieio--class-option-assoc
(list option
)
159 "Return from LIST the found OPTION, or nil if it doesn't exist."
160 `(car-safe (cdr (memq ,option
,list
))))
162 (defsubst eieio--class-option
(class option
)
163 "Return the value stored for CLASS' OPTION.
164 Return nil if that option doesn't exist."
165 (eieio--class-option-assoc (eieio--class-options class
) option
))
167 (defun eieio-object-p (obj)
168 "Return non-nil if OBJ is an EIEIO object."
171 (let ((tag (eieio--object-class-tag obj
)))
173 ;; (eq (symbol-function tag) :quick-object-witness-check)
175 (eieio--class-p (symbol-value tag
))))))
177 (define-obsolete-function-alias 'object-p
'eieio-object-p
"25.1")
179 (defun class-abstract-p (class)
180 "Return non-nil if CLASS is abstract.
181 Abstract classes cannot be instantiated."
182 (eieio--class-option (cl--find-class class
) :abstract
))
184 (defsubst eieio--class-method-invocation-order
(class)
185 "Return the invocation order of CLASS.
186 Abstract classes cannot be instantiated."
187 (or (eieio--class-option class
:method-invocation-order
)
195 (defvar eieio-defclass-autoload-map
(make-hash-table)
196 "Symbol map of superclasses we find in autoloads.")
198 ;; We autoload this because it's used in `make-autoload'.
200 (defun eieio-defclass-autoload (cname _superclasses filename doc
)
201 "Create autoload symbols for the EIEIO class CNAME.
202 SUPERCLASSES are the superclasses that CNAME inherits from.
203 DOC is the docstring for CNAME.
204 This function creates a mock-class for CNAME and adds it into
205 SUPERCLASSES as children.
206 It creates an autoload function for CNAME's constructor."
207 ;; Assume we've already debugged inputs.
209 ;; We used to store the list of superclasses in the `parent' slot (as a list
210 ;; of class names). But now this slot holds a list of class objects, and
211 ;; those parents may not exist yet, so the corresponding class objects may
212 ;; simply not exist yet. So instead we just don't store the list of parents
213 ;; here in eieio-defclass-autoload at all, since it seems that they're just
214 ;; not needed before the class is actually loaded.
215 (let* ((oldc (cl--find-class cname
))
216 (newc (eieio--class-make cname
)))
217 (if (eieio--class-p oldc
)
218 nil
;; Do nothing if we already have this class.
220 ;; turn this into a usable self-pointing symbol
221 (when eieio-backward-compatibility
223 (make-obsolete-variable cname
(format "use \\='%s instead" cname
)
226 ;; Store the new class vector definition into the symbol. We need to
227 ;; do this first so that we can call defmethod for the accessor.
228 ;; The vector will be updated by the following while loop and will not
229 ;; need to be stored a second time.
230 (setf (cl--find-class cname
) newc
)
232 ;; Create an autoload on top of our constructor function.
233 (autoload cname filename doc nil nil
)
234 (autoload (intern (format "%s-p" cname
)) filename
"" nil nil
)
235 (when eieio-backward-compatibility
236 (autoload (intern (format "%s-child-p" cname
)) filename
"" nil nil
)
237 (autoload (intern (format "%s-list-p" cname
)) filename
"" nil nil
)))))
239 (defsubst eieio-class-un-autoload
(cname)
240 "If class CNAME is in an autoload state, load its file."
241 (autoload-do-load (symbol-function cname
))) ; cname
243 (cl-deftype list-of
(elem-type)
245 (satisfies (lambda (list)
246 (cl-every (lambda (elem) (cl-typep elem
',elem-type
))
250 (defun eieio-make-class-predicate (class)
253 (format "Return non-nil if OBJ is an object of type `%S'.\n\n(fn OBJ)"
255 (and (eieio-object-p obj
)
256 (same-class-p obj class
))))
258 (defun eieio-make-child-predicate (class)
261 (format "Return non-nil if OBJ is an object of type `%S' or a subclass.
263 (and (eieio-object-p obj
)
264 (object-of-class-p obj class
))))
266 (defvar eieio--known-slot-names nil
)
268 (defun eieio-defclass-internal (cname superclasses slots options
)
269 "Define CNAME as a new subclass of SUPERCLASSES.
270 SLOTS are the slots residing in that class definition, and OPTIONS
271 holds the class options.
272 See `defclass' for more information."
273 ;; Run our eieio-hook each time, and clear it when we are done.
274 ;; This way people can add hooks safely if they want to modify eieio
275 ;; or add definitions when eieio is loaded or something like that.
276 (run-hooks 'eieio-hook
)
277 (setq eieio-hook nil
)
279 (let* ((oldc (let ((c (cl--find-class cname
))) (if (eieio--class-p c
) c
)))
281 ;; Reuse `oldc' instead of creating a new one, so that
282 ;; existing references stay valid. E.g. when
283 ;; reloading the file that does the `defclass', we don't
284 ;; want to create a new class object.
285 (eieio--class-make cname
)))
286 (groups nil
) ;; list of groups id'd from slots
289 ;; If this class already existed, and we are updating its structure,
290 ;; make sure we keep the old child list. This can cause bugs, but
291 ;; if no new slots are created, it also saves time, and prevents
292 ;; method table breakage, particularly when the users is only
293 ;; byte compiling an EIEIO file.
296 (cl-assert (eq newc oldc
))
298 (setf (eieio--class-parents newc
) nil
)
299 (setf (eieio--class-slots newc
) nil
)
300 (setf (eieio--class-initarg-tuples newc
) nil
)
301 (setf (eieio--class-class-slots newc
) nil
))
302 ;; If the old class did not exist, but did exist in the autoload map,
303 ;; then adopt those children. This is like the above, but deals with
305 (let ((children (gethash cname eieio-defclass-autoload-map
)))
307 (setf (eieio--class-children newc
) children
)
308 (remhash cname eieio-defclass-autoload-map
))))
312 (dolist (p superclasses
)
313 (if (not (and p
(symbolp p
)))
314 (error "Invalid parent class %S" p
)
315 (let ((c (cl--find-class p
)))
316 (if (not (eieio--class-p c
))
318 (error "Given parent class %S is not a class" p
)
319 ;; good parent class...
320 ;; save new child in parent
321 (cl-pushnew cname
(eieio--class-children c
))
322 ;; Get custom groups, and store them into our local copy.
323 (mapc (lambda (g) (cl-pushnew g groups
:test
#'equal
))
324 (eieio--class-option c
:custom-groups
))
325 ;; Save parent in child.
326 (push c
(eieio--class-parents newc
))))))
327 ;; Reverse the list of our parents so that they are prioritized in
328 ;; the same order as specified in the code.
329 (cl-callf nreverse
(eieio--class-parents newc
)))
330 ;; If there is nothing to loop over, then inherit from the
331 ;; default superclass.
332 (unless (eq cname
'eieio-default-superclass
)
333 ;; adopt the default parent here, but clear it later...
335 ;; save new child in parent
336 (cl-pushnew cname
(eieio--class-children eieio-default-superclass
))
337 ;; save parent in child
338 (setf (eieio--class-parents newc
) (list eieio-default-superclass
))))
340 ;; turn this into a usable self-pointing symbol; FIXME: Why?
341 (when eieio-backward-compatibility
343 (make-obsolete-variable cname
(format "use \\='%s instead" cname
)
346 ;; Create a handy list of the class test too
347 (when eieio-backward-compatibility
348 (let ((csym (intern (concat (symbol-name cname
) "-list-p"))))
352 "Test OBJ to see if it a list of objects which are a child of type %s"
355 (let ((ans t
)) ;; nil is valid
356 ;; Loop over all the elements of the input list, test
357 ;; each to make sure it is a child of the desired object class.
359 (setq ans
(and (eieio-object-p (car obj
))
360 (object-of-class-p (car obj
) ,cname
)))
361 (setq obj
(cdr obj
)))
363 (make-obsolete csym
(format
364 "use (cl-typep ... \\='(list-of %s)) instead"
368 ;; Before adding new slots, let's add all the methods and classes
369 ;; in from the parent class.
370 (eieio-copy-parents-into-subclass newc
)
372 ;; Store the new class vector definition into the symbol. We need to
373 ;; do this first so that we can call defmethod for the accessor.
374 ;; The vector will be updated by the following while loop and will not
375 ;; need to be stored a second time.
376 (setf (cl--find-class cname
) newc
)
378 ;; Query each slot in the declaration list and mangle into the
379 ;; class structure I have defined.
380 (pcase-dolist (`(,name .
,slot
) slots
)
381 (let* ((init (or (plist-get slot
:initform
)
382 (if (member :initform slot
) nil
384 (initarg (plist-get slot
:initarg
))
385 (docstr (plist-get slot
:documentation
))
386 (prot (plist-get slot
:protection
))
387 (alloc (plist-get slot
:allocation
))
388 (type (plist-get slot
:type
))
389 (custom (plist-get slot
:custom
))
390 (label (plist-get slot
:label
))
391 (customg (plist-get slot
:group
))
392 (printer (plist-get slot
:printer
))
394 (skip-nil (eieio--class-option-assoc options
:allow-nil-initform
))
397 ;; Clean up the meaning of protection.
400 ((or 'nil
'public
':public
) nil
)
401 ((or 'protected
':protected
) 'protected
)
402 ((or 'private
':private
) 'private
)
403 (_ (signal 'invalid-slot-type
(list :protection prot
)))))
405 ;; The default type specifier is supposed to be t, meaning anything.
406 (if (not type
) (setq type t
))
408 ;; intern the symbol so we can use it blankly
409 (if eieio-backward-compatibility
410 (and initarg
(not (keywordp initarg
))
412 (set initarg initarg
)
413 (make-obsolete-variable
414 initarg
(format "use \\='%s instead" initarg
) "25.1"))))
416 ;; The customgroup should be a list of symbols.
417 (cond ((and (null customg
) custom
)
418 (setq customg
'(default)))
419 ((not (listp customg
))
420 (setq customg
(list customg
))))
421 ;; The customgroup better be a list of symbols.
424 (signal 'invalid-slot-type
(list :group cg
))))
426 ;; First up, add this slot into our new class.
428 newc
(cl--make-slot-descriptor
430 `(,@(if docstr
`((:documentation .
,docstr
)))
431 ,@(if custom
`((:custom .
,custom
)))
432 ,@(if label
`((:label .
,label
)))
433 ,@(if customg
`((:group .
,customg
)))
434 ,@(if printer
`((:printer .
,printer
)))
435 ,@(if prot
`((:protection .
,prot
)))))
436 initarg alloc
'defaultoverride skip-nil
)
438 ;; We need to id the group, and store them in a group list attribute.
440 (cl-pushnew cg groups
:test
#'equal
))
443 ;; Now that everything has been loaded up, all our lists are backwards!
444 ;; Fix that up now and then them into vectors.
445 (cl-callf (lambda (slots) (apply #'vector
(nreverse slots
)))
446 (eieio--class-slots newc
))
447 (cl-callf nreverse
(eieio--class-initarg-tuples newc
))
449 ;; The storage for class-class-allocation-type needs to be turned into
451 (cl-callf (lambda (slots) (apply #'vector slots
))
452 (eieio--class-class-slots newc
))
454 ;; Also, setup the class allocated values.
455 (let* ((slots (eieio--class-class-slots newc
))
457 (v (make-vector n nil
)))
459 (setf (aref v i
) (eieio-default-eval-maybe
460 (cl--slot-descriptor-initform (aref slots i
)))))
461 (setf (eieio--class-class-allocation-values newc
) v
))
463 ;; Attach slot symbols into a hash table, and store the index of
464 ;; this slot as the value this table.
465 (let* ((slots (eieio--class-slots newc
))
466 ;; (cslots (eieio--class-class-slots newc))
467 (oa (make-hash-table :test
#'eq
)))
468 ;; (dotimes (cnt (length cslots))
469 ;; (setf (gethash (cl--slot-descriptor-name (aref cslots cnt)) oa) (- -1 cnt)))
470 (dotimes (cnt (length slots
))
471 (setf (gethash (cl--slot-descriptor-name (aref slots cnt
)) oa
) cnt
))
472 (setf (eieio--class-index-table newc
) oa
))
474 ;; Set up a specialized doc string.
475 ;; Use stored value since it is calculated in a non-trivial way
476 (let ((docstring (eieio--class-option-assoc options
:documentation
)))
477 (setf (eieio--class-docstring newc
) docstring
)
478 (when eieio-backward-compatibility
479 (put cname
'variable-documentation docstring
)))
481 ;; Save the file location where this class is defined.
482 (add-to-list 'current-load-list
`(define-type .
,cname
))
484 ;; We have a list of custom groups. Store them into the options.
485 (let ((g (eieio--class-option-assoc options
:custom-groups
)))
486 (mapc (lambda (cg) (cl-pushnew cg g
:test
'equal
)) groups
)
487 (if (memq :custom-groups options
)
488 (setcar (cdr (memq :custom-groups options
)) g
)
489 (setq options
(cons :custom-groups
(cons g options
)))))
491 ;; Set up the options we have collected.
492 (setf (eieio--class-options newc
) options
)
494 ;; if this is a superclass, clear out parent (which was set to the
495 ;; default superclass eieio-default-superclass)
496 (if clearparent
(setf (eieio--class-parents newc
) nil
))
498 ;; Create the cached default object.
499 (let ((cache (make-vector (+ (length (eieio--class-slots newc
))
500 (eval-when-compile eieio--object-num-slots
))
502 ;; We don't strictly speaking need to use a symbol, but the old
503 ;; code used the class's name rather than the class's object, so
504 ;; we follow this preference for using a symbol, which is probably
505 ;; convenient to keep the printed representation of such Elisp
507 (tag (intern (format "eieio-class-tag--%s" cname
))))
509 (fset tag
:quick-object-witness-check
)
510 (setf (eieio--object-class-tag cache
) tag
)
511 (let ((eieio-skip-typecheck t
))
512 ;; All type-checking has been done to our satisfaction
513 ;; before this call. Don't waste our time in this call..
514 (eieio-set-defaults cache t
))
515 (setf (eieio--class-default-object-cache newc
) cache
))
517 ;; Return our new class object
522 (defsubst eieio-eval-default-p
(val)
523 "Whether the default value VAL should be evaluated for use."
524 (and (consp val
) (symbolp (car val
)) (fboundp (car val
))))
526 (defun eieio--perform-slot-validation-for-default (slot skipnil
)
527 "For SLOT, signal if its type does not match its default value.
528 If SKIPNIL is non-nil, then if default value is nil return t instead."
529 (let ((value (cl--slot-descriptor-initform slot
))
530 (spec (cl--slot-descriptor-type slot
)))
531 (if (not (or (eieio-eval-default-p value
) ;FIXME: Why?
533 (and skipnil
(null value
))
534 (eieio--perform-slot-validation spec value
)))
535 (signal 'invalid-slot-type
(list (cl--slot-descriptor-name slot
) spec value
)))))
537 (defun eieio--slot-override (old new skipnil
)
538 (cl-assert (eq (cl--slot-descriptor-name old
) (cl--slot-descriptor-name new
)))
539 ;; There is a match, and we must override the old value.
540 (let* ((a (cl--slot-descriptor-name old
))
541 (tp (cl--slot-descriptor-type old
))
542 (d (cl--slot-descriptor-initform new
))
543 (type (cl--slot-descriptor-type new
))
544 (oprops (cl--slot-descriptor-props old
))
545 (nprops (cl--slot-descriptor-props new
))
546 (custg (alist-get :group nprops
)))
547 ;; If type is passed in, is it the same?
548 (if (not (eq type t
))
549 (if (not (equal type tp
))
551 "Child slot type `%s' does not match inherited type `%s' for `%s'"
553 (setf (cl--slot-descriptor-type new
) tp
))
554 ;; If we have a repeat, only update the initarg...
555 (unless (eq d eieio-unbound
)
556 (eieio--perform-slot-validation-for-default new skipnil
)
557 (setf (cl--slot-descriptor-initform old
) d
))
559 ;; PLN Tue Jun 26 11:57:06 2007 : The protection is
560 ;; checked and SHOULD match the superclass
561 ;; protection. Otherwise an error is thrown. However
562 ;; I wonder if a more flexible schedule might be
565 ;; EML - We used to have (if prot... here,
566 ;; but a prot of 'nil means public.
568 (let ((super-prot (alist-get :protection oprops
))
569 (prot (alist-get :protection nprops
)))
570 (if (not (eq prot super-prot
))
571 (error "Child slot protection `%s' does not match inherited protection `%s' for `%s'"
575 ;; PLN Tue Jun 26 11:57:06 2007 :
576 ;; Do a non redundant combination of ancient custom
577 ;; groups and new ones.
579 (let* ((list1 (alist-get :group oprops
)))
581 (unless (memq elt list1
)
583 (setf (alist-get :group
(cl--slot-descriptor-props old
)) list1
)))
586 ;; PLN Mon Jun 25 22:44:34 2007 : If a new cust is
587 ;; set, simply replaces the old one.
588 (dolist (prop '(:custom
:label
:documentation
:printer
))
589 (when (alist-get prop
(cl--slot-descriptor-props new
))
590 (setf (alist-get prop
(cl--slot-descriptor-props old
))
591 (alist-get prop
(cl--slot-descriptor-props new
))))
595 (defun eieio--add-new-slot (newc slot init alloc
596 &optional defaultoverride skipnil
)
597 "Add into NEWC attribute SLOT.
598 If a slot of that name already exists in NEWC, then do nothing. If it doesn't exist,
599 INIT is the initarg, if any.
600 Argument ALLOC specifies if the slot is allocated per instance, or per class.
601 If optional DEFAULTOVERRIDE is non-nil, then if A exists in NEWC,
602 we must override its value for a default.
603 Optional argument SKIPNIL indicates if type checking should be skipped
604 if default value is nil."
605 ;; Make sure we duplicate those items that are sequences.
606 (let* ((a (cl--slot-descriptor-name slot
))
607 (d (cl--slot-descriptor-initform slot
))
608 (old (car (cl-member a
(eieio--class-slots newc
)
609 :key
#'cl--slot-descriptor-name
)))
610 (cold (car (cl-member a
(eieio--class-class-slots newc
)
611 :key
#'cl--slot-descriptor-name
))))
612 (cl-pushnew a eieio--known-slot-names
)
614 (if (sequencep d
) (setq d
(copy-sequence d
)))
615 ;; This copy can fail on a cons cell with a non-cons in the cdr. Let's
616 ;; skip it if it doesn't work.
618 ;; (if (sequencep type) (setq type (copy-sequence type)))
619 ;; (if (sequencep cust) (setq cust (copy-sequence cust)))
620 ;; (if (sequencep custg) (setq custg (copy-sequence custg)))
622 ;; To prevent override information w/out specification of storage,
623 ;; we need to do this little hack.
624 (if cold
(setq alloc
:class
))
626 (if (memq alloc
'(nil :instance
))
627 ;; In this case, we modify the INSTANCE version of a given slot.
629 ;; Only add this element if it is so-far unique
632 (eieio--perform-slot-validation-for-default slot skipnil
)
633 (push slot
(eieio--class-slots newc
))
635 ;; When defaultoverride is true, we are usually adding new local
636 ;; attributes which must override the default value of any slot
637 ;; passed in by one of the parent classes.
638 (when defaultoverride
639 (eieio--slot-override old slot skipnil
)))
641 (cl-pushnew (cons init a
) (eieio--class-initarg-tuples newc
)
644 ;; CLASS ALLOCATED SLOTS
647 (eieio--perform-slot-validation-for-default slot skipnil
)
648 ;; Here we have found a :class version of a slot. This
649 ;; requires a very different approach.
650 (push slot
(eieio--class-class-slots newc
)))
651 (when defaultoverride
652 ;; There is a match, and we must override the old value.
653 (eieio--slot-override cold slot skipnil
))))))
655 (defun eieio-copy-parents-into-subclass (newc)
656 "Copy into NEWC the slots of PARENTS.
657 Follow the rules of not overwriting early parents when applying to
658 the new child class."
659 (let ((sn (eieio--class-option-assoc (eieio--class-options newc
)
660 :allow-nil-initform
)))
661 (dolist (pcv (eieio--class-parents newc
))
662 ;; First, duplicate all the slots of the parent.
663 (let ((pslots (eieio--class-slots pcv
))
664 (pinit (eieio--class-initarg-tuples pcv
)))
665 (dotimes (i (length pslots
))
666 (let* ((sd (cl--copy-slot-descriptor (aref pslots i
)))
667 (init (car (rassq (cl--slot-descriptor-name sd
) pinit
))))
668 (eieio--add-new-slot newc sd init nil nil sn
))
670 ;; Now duplicate all the class alloc slots.
671 (let ((pcslots (eieio--class-class-slots pcv
)))
672 (dotimes (i (length pcslots
))
673 (eieio--add-new-slot newc
(cl--copy-slot-descriptor
679 ;;; Slot type validation
681 ;; This is a hideous hack for replacing `typep' from cl-macs, to avoid
682 ;; requiring the CL library at run-time. It can be eliminated if/when
683 ;; `typep' is merged into Emacs core.
685 (defun eieio--perform-slot-validation (spec value
)
686 "Return non-nil if SPEC does not match VALUE."
687 (or (eq spec t
) ; t always passes
688 (eq value eieio-unbound
) ; unbound always passes
689 (cl-typep value spec
)))
691 (defun eieio--validate-slot-value (class slot-idx value slot
)
692 "Make sure that for CLASS referencing SLOT-IDX, VALUE is valid.
693 Checks the :type specifier.
694 SLOT is the slot that is being checked, and is only used when throwing
696 (if eieio-skip-typecheck
698 ;; Trim off object IDX junk added in for the object index.
699 (setq slot-idx
(- slot-idx
(eval-when-compile eieio--object-num-slots
)))
700 (let ((st (cl--slot-descriptor-type (aref (eieio--class-slots class
)
702 (if (not (eieio--perform-slot-validation st value
))
703 (signal 'invalid-slot-type
704 (list (eieio--class-name class
) slot st value
))))))
706 (defun eieio--validate-class-slot-value (class slot-idx value slot
)
707 "Make sure that for CLASS referencing SLOT-IDX, VALUE is valid.
708 Checks the :type specifier.
709 SLOT is the slot that is being checked, and is only used when throwing
711 (if eieio-skip-typecheck
713 (let ((st (cl--slot-descriptor-type (aref (eieio--class-class-slots class
)
715 (if (not (eieio--perform-slot-validation st value
))
716 (signal 'invalid-slot-type
717 (list (eieio--class-name class
) slot st value
))))))
719 (defun eieio-barf-if-slot-unbound (value instance slotname fn
)
720 "Throw a signal if VALUE is a representation of an UNBOUND slot.
721 INSTANCE is the object being referenced. SLOTNAME is the offending
722 slot. If the slot is ok, return VALUE.
723 Argument FN is the function calling this verifier."
724 (if (and (eq value eieio-unbound
) (not eieio-skip-typecheck
))
725 (slot-unbound instance
(eieio--object-class instance
) slotname fn
)
729 ;;; Get/Set slots in an object.
731 (defun eieio-oref (obj slot
)
732 "Return the value in OBJ at SLOT in the object vector."
733 (declare (compiler-macro
737 ((and (or `',name
(and name
(pred keywordp
)))
738 (guard (not (memq name eieio--known-slot-names
))))
739 (macroexp--warn-and-return
740 (format-message "Unknown slot `%S'" name
) exp
'compile-only
))
742 (cl-check-type slot symbol
)
743 (cl-check-type obj
(or eieio-object class
))
744 (let* ((class (cond ((symbolp obj
)
745 (error "eieio-oref called on a class: %s" obj
)
746 (let ((c (cl--find-class obj
)))
747 (if (eieio--class-p c
) (eieio-class-un-autoload obj
))
749 (t (eieio--object-class obj
))))
750 (c (eieio--slot-name-index class slot
)))
752 ;; It might be missing because it is a :class allocated slot.
753 ;; Let's check that info out.
754 (if (setq c
(eieio--class-slot-name-index class slot
))
756 (aref (eieio--class-class-allocation-values class
) c
)
757 ;; The slot-missing method is a cool way of allowing an object author
758 ;; to intercept missing slot definitions. Since it is also the LAST
759 ;; thing called in this fn, its return value would be retrieved.
760 (slot-missing obj slot
'oref
))
761 (cl-check-type obj eieio-object
)
762 (eieio-barf-if-slot-unbound (aref obj c
) obj slot
'oref
))))
765 (defun eieio-oref-default (obj slot
)
766 "Do the work for the macro `oref-default' with similar parameters.
767 Fills in OBJ's SLOT with its default value."
768 (cl-check-type obj
(or eieio-object class
))
769 (cl-check-type slot symbol
)
770 (let* ((cl (cond ((symbolp obj
) (cl--find-class obj
))
771 ((eieio-object-p obj
) (eieio--object-class obj
))
773 (c (eieio--slot-name-index cl slot
)))
775 ;; It might be missing because it is a :class allocated slot.
776 ;; Let's check that info out.
778 (eieio--class-slot-name-index cl slot
))
780 (aref (eieio--class-class-allocation-values cl
)
782 (slot-missing obj slot
'oref-default
))
783 (eieio-barf-if-slot-unbound
784 (let ((val (cl--slot-descriptor-initform
785 (aref (eieio--class-slots cl
)
786 (- c
(eval-when-compile eieio--object-num-slots
))))))
787 (eieio-default-eval-maybe val
))
788 obj
(eieio--class-name cl
) 'oref-default
))))
790 (defun eieio-default-eval-maybe (val)
791 "Check VAL, and return what `oref-default' would provide."
792 ;; FIXME: What the hell is this supposed to do? Shouldn't it evaluate
793 ;; variables as well? Why not just always call `eval'?
795 ;; Is it a function call? If so, evaluate it.
796 ((eieio-eval-default-p val
)
798 ;;;; check for quoted things, and unquote them
799 ;;((and (consp val) (eq (car val) 'quote))
801 ;; return it verbatim
804 (defun eieio-oset (obj slot value
)
805 "Do the work for the macro `oset'.
806 Fills in OBJ's SLOT with VALUE."
807 (cl-check-type obj eieio-object
)
808 (cl-check-type slot symbol
)
809 (let* ((class (eieio--object-class obj
))
810 (c (eieio--slot-name-index class slot
)))
812 ;; It might be missing because it is a :class allocated slot.
813 ;; Let's check that info out.
815 (eieio--class-slot-name-index class slot
))
818 (eieio--validate-class-slot-value class c value slot
)
819 (aset (eieio--class-class-allocation-values class
)
821 ;; See oref for comment on `slot-missing'
822 (slot-missing obj slot
'oset value
))
823 (eieio--validate-slot-value class c value slot
)
824 (aset obj c value
))))
826 (defun eieio-oset-default (class slot value
)
827 "Do the work for the macro `oset-default'.
828 Fills in the default value in CLASS' in SLOT with VALUE."
829 (setq class
(eieio--class-object class
))
830 (cl-check-type class eieio--class
)
831 (cl-check-type slot symbol
)
832 (let* ((c (eieio--slot-name-index class slot
)))
834 ;; It might be missing because it is a :class allocated slot.
835 ;; Let's check that info out.
836 (if (setq c
(eieio--class-slot-name-index class slot
))
839 (eieio--validate-class-slot-value class c value slot
)
840 (aset (eieio--class-class-allocation-values class
) c
842 (signal 'invalid-slot-name
(list (eieio--class-name class
) slot
)))
843 ;; `oset-default' on an instance-allocated slot is allowed by EIEIO but
844 ;; not by CLOS and is mildly inconsistent with the :initform thingy, so
845 ;; it'd be nice to get of it. This said, it is/was used at one place by
846 ;; gnus/registry.el, so it might be used elsewhere as well, so let's
848 ;; FIXME: Generate a compile-time warning for it!
849 ;; (error "Can't `oset-default' an instance-allocated slot: %S of %S"
851 (eieio--validate-slot-value class c value slot
)
852 ;; Set this into the storage for defaults.
853 (if (eieio-eval-default-p value
)
854 (error "Can't set default to a sexp that gets evaluated again"))
855 (setf (cl--slot-descriptor-initform
856 ;; FIXME: Apparently we set it both in `slots' and in
857 ;; `object-cache', which seems redundant.
858 (aref (eieio--class-slots class
)
859 (- c
(eval-when-compile eieio--object-num-slots
))))
861 ;; Take the value, and put it into our cache object.
862 (eieio-oset (eieio--class-default-object-cache class
)
867 ;;; EIEIO internal search functions
869 (defun eieio--slot-name-index (class slot
)
870 "In CLASS find the index of the named SLOT.
871 The slot is a symbol which is installed in CLASS by the `defclass' call.
872 If SLOT is the value created with :initarg instead,
873 reverse-lookup that name, and recurse with the associated slot value."
874 ;; Removed checks to outside this call
875 (let* ((fsi (gethash slot
(eieio--class-index-table class
))))
877 (+ (eval-when-compile eieio--object-num-slots
) fsi
)
878 (let ((fn (eieio--initarg-to-attribute class slot
)))
880 ;; Accessing a slot via its :initarg is accepted by EIEIO
881 ;; (but not CLOS) but is a bad idea (for one: it's slower).
882 ;; FIXME: We should emit a compile-time warning when this happens!
883 (eieio--slot-name-index class fn
)
886 (defun eieio--class-slot-name-index (class slot
)
887 "In CLASS find the index of the named SLOT.
888 The slot is a symbol which is installed in CLASS by the `defclass'
889 call. If SLOT is the value created with :initarg instead,
890 reverse-lookup that name, and recurse with the associated slot value."
891 ;; This will happen less often, and with fewer slots. Do this the
892 ;; storage cheap way.
894 (slots (eieio--class-class-slots class
)))
895 (dotimes (i (length slots
))
896 (if (eq slot
(cl--slot-descriptor-name (aref slots i
)))
901 ;; Way to assign slots based on a list. Used for constructors, or
902 ;; even resetting an object at run-time
904 (defun eieio-set-defaults (obj &optional set-all
)
905 "Take object OBJ, and reset all slots to their defaults.
906 If SET-ALL is non-nil, then when a default is nil, that value is
907 reset. If SET-ALL is nil, the slots are only reset if the default is
909 (let ((slots (eieio--class-slots (eieio--object-class obj
))))
910 (dotimes (i (length slots
))
911 (let* ((name (cl--slot-descriptor-name (aref slots i
)))
912 (df (eieio-oref-default obj name
)))
914 (eieio-oset obj name df
))))))
916 (defun eieio--initarg-to-attribute (class initarg
)
917 "For CLASS, convert INITARG to the actual attribute name.
918 If there is no translation, pass it in directly (so we can cheat if
919 need be... May remove that later...)"
920 (let ((tuple (assoc initarg
(eieio--class-initarg-tuples class
))))
926 ;; Method Invocation order: C3
927 (defun eieio--c3-candidate (class remaining-inputs
)
928 "Return CLASS if it can go in the result now, otherwise nil."
929 ;; Ensure CLASS is not in any position but the first in any of the
930 ;; element lists of REMAINING-INPUTS.
931 (and (not (let ((found nil
))
932 (while (and remaining-inputs
(not found
))
933 (setq found
(member class
(cdr (car remaining-inputs
)))
934 remaining-inputs
(cdr remaining-inputs
)))
938 (defun eieio--c3-merge-lists (reversed-partial-result remaining-inputs
)
939 "Merge REVERSED-PARTIAL-RESULT REMAINING-INPUTS in a consistent order, if possible.
940 If a consistent order does not exist, signal an error."
941 (setq remaining-inputs
(delq nil remaining-inputs
))
942 (if (null remaining-inputs
)
943 ;; If all remaining inputs are empty lists, we are done.
944 (nreverse reversed-partial-result
)
945 ;; Otherwise, we try to find the next element of the result. This
946 ;; is achieved by considering the first element of each
947 ;; (non-empty) input list and accepting a candidate if it is
948 ;; consistent with the rests of the input lists.
950 (tail remaining-inputs
)
952 (while (and tail
(not found
))
953 (setq found
(eieio--c3-candidate (caar tail
)
958 ;; The graph is consistent so far, add NEXT to result and
959 ;; merge input lists, dropping NEXT from their heads where
961 (eieio--c3-merge-lists
962 (cons next reversed-partial-result
)
963 (mapcar (lambda (l) (if (eq (cl-first l
) next
) (cl-rest l
) l
))
965 ;; The graph is inconsistent, give up
966 (signal 'inconsistent-class-hierarchy
(list remaining-inputs
))))))
968 (defsubst eieio--class
/struct-parents
(class)
969 (or (eieio--class-parents class
)
970 `(,eieio-default-superclass
)))
972 (defun eieio--class-precedence-c3 (class)
973 "Return all parents of CLASS in c3 order."
974 (let ((parents (eieio--class-parents class
)))
975 (eieio--c3-merge-lists
979 (mapcar #'eieio--class-precedence-c3 parents
)
980 `((,eieio-default-superclass
)))
984 ;; Method Invocation Order: Depth First
986 (defun eieio--class-precedence-dfs (class)
987 "Return all parents of CLASS in depth-first order."
988 (let* ((parents (eieio--class-parents class
))
989 (classes (copy-sequence
996 (eieio--class-precedence-dfs parent
)))
998 `((,eieio-default-superclass
))))))
1000 ;; Remove duplicates.
1002 (setcdr tail
(delq (car tail
) (cdr tail
)))
1003 (setq tail
(cdr tail
)))
1007 ;; Method Invocation Order: Breadth First
1008 (defun eieio--class-precedence-bfs (class)
1009 "Return all parents of CLASS in breadth-first order."
1011 (queue (eieio--class/struct-parents class
)))
1013 (let ((head (pop queue
)))
1014 (unless (member head result
)
1016 (unless (eq head eieio-default-superclass
)
1017 (setq queue
(append queue
(eieio--class/struct-parents head
)))))))
1018 (cons class
(nreverse result
)))
1022 ;; Method Invocation Order
1024 (defun eieio--class-precedence-list (class)
1025 "Return (transitively closed) list of parents of CLASS.
1026 The order, in which the parents are returned depends on the
1027 method invocation orders of the involved classes."
1028 (if (or (null class
) (eq class eieio-default-superclass
))
1030 (unless (eieio--class-default-object-cache class
)
1031 (eieio-class-un-autoload (eieio--class-name class
)))
1032 (cl-case (eieio--class-method-invocation-order class
)
1034 (eieio--class-precedence-dfs class
))
1036 (eieio--class-precedence-bfs class
))
1038 (eieio--class-precedence-c3 class
))))
1040 (define-obsolete-function-alias
1041 'class-precedence-list
'eieio--class-precedence-list
"24.4")
1044 ;;; Here are some special types of errors
1046 (define-error 'invalid-slot-name
"Invalid slot name")
1047 (define-error 'invalid-slot-type
"Invalid slot type")
1048 (define-error 'unbound-slot
"Unbound slot")
1049 (define-error 'inconsistent-class-hierarchy
"Inconsistent class hierarchy")
1051 ;;; Hooking into cl-generic.
1053 (require 'cl-generic
)
1055 ;;;; General support to dispatch based on the type of the argument.
1057 (cl-generic-define-generalizer eieio--generic-generalizer
1058 ;; Use the exact same tagcode as for cl-struct, so that methods
1059 ;; that dispatch on both kinds of objects get to share this
1060 ;; part of the dispatch code.
1061 50 #'cl--generic-struct-tag
1062 (lambda (tag &rest _
)
1063 (and (symbolp tag
) (boundp tag
) (eieio--class-p (symbol-value tag
))
1064 (mapcar #'eieio--class-name
1065 (eieio--class-precedence-list (symbol-value tag
))))))
1067 (cl-defmethod cl-generic-generalizers :extra
"class" (specializer)
1068 "Support for dispatch on types defined by EIEIO's `defclass'."
1070 ;; A class must be defined before it can be used as a parameter
1071 ;; specializer in a defmethod form.
1072 ;; So we can ignore types that are not known to denote classes.
1074 (and (eieio--class-p (eieio--class-object specializer
))
1075 (list eieio--generic-generalizer
))
1076 (cl-call-next-method)))
1078 ;;;; Dispatch for arguments which are classes.
1080 ;; Since EIEIO does not support metaclasses, users can't easily use the
1081 ;; "dispatch on argument type" for class arguments. That's why EIEIO's
1082 ;; `defmethod' added the :static qualifier. For cl-generic, such a qualifier
1083 ;; would not make much sense (e.g. to which argument should it apply?).
1084 ;; Instead, we add a new "subclass" specializer.
1086 (defun eieio--generic-subclass-specializers (tag &rest _
)
1087 (when (eieio--class-p tag
)
1088 (mapcar (lambda (class)
1089 `(subclass ,(eieio--class-name class
)))
1090 (eieio--class-precedence-list tag
))))
1092 (cl-generic-define-generalizer eieio--generic-subclass-generalizer
1093 60 (lambda (name &rest _
) `(and (symbolp ,name
) (cl--find-class ,name
)))
1094 #'eieio--generic-subclass-specializers
)
1096 (cl-defmethod cl-generic-generalizers ((_specializer (head subclass
)))
1097 "Support for (subclass CLASS) specializers.
1098 These match if the argument is the name of a subclass of CLASS."
1099 (list eieio--generic-subclass-generalizer
))
1101 (provide 'eieio-core
)
1103 ;;; eieio-core.el ends here