1 ;;; eieio-core.el --- Core implementation for eieio -*- lexical-binding:t -*-
3 ;; Copyright (C) 1995-1996, 1998-2015 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.
38 ;; A few functions that are better in the official EIEIO src, but
39 ;; used from the core.
40 (declare-function slot-unbound "eieio")
41 (declare-function slot-missing "eieio")
42 (declare-function child-of-class-p "eieio")
43 (declare-function same-class-p "eieio")
44 (declare-function object-of-class-p "eieio")
48 ;; Variable declarations.
50 (defvar eieio-hook nil
51 "This hook is executed, then cleared each time `defclass' is called.")
53 (defvar eieio-error-unsupported-class-tags nil
54 "Non-nil to throw an error if an encountered tag is unsupported.
55 This may prevent classes from CLOS applications from being used with EIEIO
56 since EIEIO does not support all CLOS tags.")
58 (defvar eieio-skip-typecheck nil
59 "If non-nil, skip all slot typechecking.
60 Set this to t permanently if a program is functioning well to get a
61 small speed increase. This variable is also used internally to handle
62 default setting for optimization purposes.")
64 (defvar eieio-optimize-primary-methods-flag t
65 "Non-nil means to optimize the method dispatch on primary methods.")
67 (defvar eieio-backward-compatibility t
68 "If nil, drop support for some behaviors of older versions of EIEIO.
69 Currently under control of this var:
70 - Define every class as a var whose value is the class symbol.
71 - Define <class>-child-p and <class>-list-p predicates.
72 - Allow object names in constructors.")
74 (defconst eieio-unbound
75 (if (and (boundp 'eieio-unbound) (symbolp eieio-unbound))
77 (make-symbol "unbound"))
78 "Uninterned symbol representing an unbound slot in an object.")
80 ;; This is a bootstrap for eieio-default-superclass so it has a value
81 ;; while it is being built itself.
82 (defvar eieio-default-superclass nil)
85 ;; Arrange for field access not to bother checking if the access is indeed
86 ;; made to an eieio--class object.
87 (cl-declaim (optimize (safety 0)))
89 (cl-defstruct (eieio--class
91 (:constructor eieio--class-make (name))
95 initarg-tuples ;; initarg tuples list
96 (class-slots nil :type eieio--slot)
97 class-allocation-values ;; class allocated value vector
98 default-object-cache ;; what a newly created object would look like.
99 ; This will speed up instantiation time as
100 ; only a `copy-sequence' will be needed, instead of
101 ; looping over all the values and setting them from
103 options ;; storage location of tagged class option
104 ; Stored outright without modifications or stripping
106 ;; Set it back to the default value.
107 (cl-declaim (optimize (safety 1))))
110 (cl-defstruct (eieio--object
111 (:type vector) ;We manage our own tagging system.
114 ;; `class-tag' holds a symbol, which is not the class name, but is instead
115 ;; properly prefixed as an internal EIEIO thingy and which holds the class
116 ;; object/struct in its `symbol-value' slot.
120 (defconst eieio--object-num-slots
121 (length (cl-struct-slot-info 'eieio--object))))
123 (defsubst eieio--object-class (obj)
124 (symbol-value (eieio--object-class-tag obj)))
127 ;;; Important macros used internally in eieio.
129 (require 'cl-macs) ;For cl--find-class.
131 (defsubst eieio--class-object (class)
132 "Return the class object."
134 ;; Keep the symbol if class-v is nil, for better error messages.
135 (or (cl--find-class class) class)
138 (defun class-p (class)
139 "Return non-nil if CLASS is a valid class vector.
140 CLASS is a symbol." ;FIXME: Is it a vector or a symbol?
141 (and (symbolp class) (eieio--class-p (cl--find-class class))))
143 (defun eieio--class-print-name (class)
144 "Return a printed representation of CLASS."
145 (format "#<class %s>" (eieio-class-name class)))
147 (defun eieio-class-name (class)
148 "Return a Lisp like symbol name for CLASS."
149 (setq class (eieio--class-object class))
150 (cl-check-type class eieio--class)
151 (eieio--class-name class))
152 (define-obsolete-function-alias 'class-name #'eieio-class-name "24.4")
154 (defalias 'eieio--class-constructor #'identity
155 "Return the symbol representing the constructor of CLASS.")
157 (defmacro eieio--class-option-assoc (list option)
158 "Return from LIST the found OPTION, or nil if it doesn't exist."
159 `(car-safe (cdr (memq ,option ,list))))
161 (defsubst eieio--class-option (class option)
162 "Return the value stored for CLASS' OPTION.
163 Return nil if that option doesn't exist."
164 (eieio--class-option-assoc (eieio--class-options class) option))
166 (defun eieio-object-p (obj)
167 "Return non-nil if OBJ is an EIEIO object."
170 (let ((tag (eieio--object-class-tag obj)))
172 ;; (eq (symbol-function tag) :quick-object-witness-check)
174 (eieio--class-p (symbol-value tag))))))
176 (define-obsolete-function-alias 'object-p 'eieio-object-p "25.1")
178 (defun class-abstract-p (class)
179 "Return non-nil if CLASS is abstract.
180 Abstract classes cannot be instantiated."
181 (eieio--class-option (cl--find-class class) :abstract))
183 (defsubst eieio--class-method-invocation-order (class)
184 "Return the invocation order of CLASS.
185 Abstract classes cannot be instantiated."
186 (or (eieio--class-option class :method-invocation-order)
194 (defvar eieio-defclass-autoload-map (make-hash-table)
195 "Symbol map of superclasses we find in autoloads.")
197 ;; We autoload this because it's used in `make-autoload'.
199 (defun eieio-defclass-autoload (cname _superclasses filename doc)
200 "Create autoload symbols for the EIEIO class CNAME.
201 SUPERCLASSES are the superclasses that CNAME inherits from.
202 DOC is the docstring for CNAME.
203 This function creates a mock-class for CNAME and adds it into
204 SUPERCLASSES as children.
205 It creates an autoload function for CNAME's constructor."
206 ;; Assume we've already debugged inputs.
208 ;; We used to store the list of superclasses in the `parent' slot (as a list
209 ;; of class names). But now this slot holds a list of class objects, and
210 ;; those parents may not exist yet, so the corresponding class objects may
211 ;; simply not exist yet. So instead we just don't store the list of parents
212 ;; here in eieio-defclass-autoload at all, since it seems that they're just
213 ;; not needed before the class is actually loaded.
214 (let* ((oldc (cl--find-class cname))
215 (newc (eieio--class-make cname)))
216 (if (eieio--class-p oldc)
217 nil ;; Do nothing if we already have this class.
219 ;; turn this into a usable self-pointing symbol
220 (when eieio-backward-compatibility
222 (make-obsolete-variable cname (format "use \\='%s instead" cname)
225 ;; Store the new class vector definition into the symbol. We need to
226 ;; do this first so that we can call defmethod for the accessor.
227 ;; The vector will be updated by the following while loop and will not
228 ;; need to be stored a second time.
229 (setf (cl--find-class cname) newc)
231 ;; Create an autoload on top of our constructor function.
232 (autoload cname filename doc nil nil)
233 (autoload (intern (format "%s-p" cname)) filename "" nil nil)
234 (when eieio-backward-compatibility
235 (autoload (intern (format "%s-child-p" cname)) filename "" nil nil)
236 (autoload (intern (format "%s-list-p" cname)) filename "" nil nil)))))
238 (defsubst eieio-class-un-autoload (cname)
239 "If class CNAME is in an autoload state, load its file."
240 (autoload-do-load (symbol-function cname))) ; cname
242 (cl-deftype list-of (elem-type)
244 (satisfies (lambda (list)
245 (cl-every (lambda (elem) (cl-typep elem ',elem-type))
249 (defun eieio-make-class-predicate (class)
252 (format "Return non-nil if OBJ is an object of type `%S'.\n\n(fn OBJ)"
254 (and (eieio-object-p obj)
255 (same-class-p obj class))))
257 (defun eieio-make-child-predicate (class)
260 (format "Return non-nil if OBJ is an object of type `%S' or a subclass.
262 (and (eieio-object-p obj)
263 (object-of-class-p obj class))))
265 (defvar eieio--known-slot-names nil)
267 (defun eieio-defclass-internal (cname superclasses slots options)
268 "Define CNAME as a new subclass of SUPERCLASSES.
269 SLOTS are the slots residing in that class definition, and OPTIONS
270 holds the class options.
271 See `defclass' for more information."
272 ;; Run our eieio-hook each time, and clear it when we are done.
273 ;; This way people can add hooks safely if they want to modify eieio
274 ;; or add definitions when eieio is loaded or something like that.
275 (run-hooks 'eieio-hook)
276 (setq eieio-hook nil)
278 (let* ((oldc (let ((c (cl--find-class cname))) (if (eieio--class-p c) c)))
280 ;; Reuse `oldc' instead of creating a new one, so that
281 ;; existing references stay valid. E.g. when
282 ;; reloading the file that does the `defclass', we don't
283 ;; want to create a new class object.
284 (eieio--class-make cname)))
285 (groups nil) ;; list of groups id'd from slots
288 ;; If this class already existed, and we are updating its structure,
289 ;; make sure we keep the old child list. This can cause bugs, but
290 ;; if no new slots are created, it also saves time, and prevents
291 ;; method table breakage, particularly when the users is only
292 ;; byte compiling an EIEIO file.
295 (cl-assert (eq newc oldc))
297 (setf (eieio--class-parents newc) nil)
298 (setf (eieio--class-slots newc) nil)
299 (setf (eieio--class-initarg-tuples newc) nil)
300 (setf (eieio--class-class-slots newc) nil))
301 ;; If the old class did not exist, but did exist in the autoload map,
302 ;; then adopt those children. This is like the above, but deals with
304 (let ((children (gethash cname eieio-defclass-autoload-map)))
306 (setf (eieio--class-children newc) children)
307 (remhash cname eieio-defclass-autoload-map))))
311 (dolist (p superclasses)
312 (if (not (and p (symbolp p)))
313 (error "Invalid parent class %S" p)
314 (let ((c (cl--find-class p)))
315 (if (not (eieio--class-p c))
317 (error "Given parent class %S is not a class" p)
318 ;; good parent class...
319 ;; save new child in parent
320 (cl-pushnew cname (eieio--class-children c))
321 ;; Get custom groups, and store them into our local copy.
322 (mapc (lambda (g) (cl-pushnew g groups :test #'equal))
323 (eieio--class-option c :custom-groups))
324 ;; Save parent in child.
325 (push c (eieio--class-parents newc))))))
326 ;; Reverse the list of our parents so that they are prioritized in
327 ;; the same order as specified in the code.
328 (cl-callf nreverse (eieio--class-parents newc)))
329 ;; If there is nothing to loop over, then inherit from the
330 ;; default superclass.
331 (unless (eq cname 'eieio-default-superclass)
332 ;; adopt the default parent here, but clear it later...
334 ;; save new child in parent
335 (cl-pushnew cname (eieio--class-children eieio-default-superclass))
336 ;; save parent in child
337 (setf (eieio--class-parents newc) (list eieio-default-superclass))))
339 ;; turn this into a usable self-pointing symbol; FIXME: Why?
340 (when eieio-backward-compatibility
342 (make-obsolete-variable cname (format "use \\='%s instead" cname)
345 ;; Create a handy list of the class test too
346 (when eieio-backward-compatibility
347 (let ((csym (intern (concat (symbol-name cname) "-list-p"))))
351 "Test OBJ to see if it a list of objects which are a child of type %s"
354 (let ((ans t)) ;; nil is valid
355 ;; Loop over all the elements of the input list, test
356 ;; each to make sure it is a child of the desired object class.
358 (setq ans (and (eieio-object-p (car obj))
359 (object-of-class-p (car obj) ,cname)))
360 (setq obj (cdr obj)))
362 (make-obsolete csym (format
363 "use (cl-typep ... \\='(list-of %s)) instead"
367 ;; Before adding new slots, let's add all the methods and classes
368 ;; in from the parent class.
369 (eieio-copy-parents-into-subclass newc)
371 ;; Store the new class vector definition into the symbol. We need to
372 ;; do this first so that we can call defmethod for the accessor.
373 ;; The vector will be updated by the following while loop and will not
374 ;; need to be stored a second time.
375 (setf (cl--find-class cname) newc)
377 ;; Query each slot in the declaration list and mangle into the
378 ;; class structure I have defined.
379 (pcase-dolist (`(,name . ,slot) slots)
380 (let* ((init (or (plist-get slot :initform)
381 (if (member :initform slot) nil
383 (initarg (plist-get slot :initarg))
384 (docstr (plist-get slot :documentation))
385 (prot (plist-get slot :protection))
386 (alloc (plist-get slot :allocation))
387 (type (plist-get slot :type))
388 (custom (plist-get slot :custom))
389 (label (plist-get slot :label))
390 (customg (plist-get slot :group))
391 (printer (plist-get slot :printer))
393 (skip-nil (eieio--class-option-assoc options :allow-nil-initform))
396 ;; Clean up the meaning of protection.
399 ((or 'nil 'public ':public) nil)
400 ((or 'protected ':protected) 'protected)
401 ((or 'private ':private) 'private)
402 (_ (signal 'invalid-slot-type (list :protection prot)))))
404 ;; The default type specifier is supposed to be t, meaning anything.
405 (if (not type) (setq type t))
407 ;; intern the symbol so we can use it blankly
408 (if eieio-backward-compatibility
409 (and initarg (not (keywordp initarg))
411 (set initarg initarg)
412 (make-obsolete-variable
413 initarg (format "use \\='%s instead" initarg) "25.1"))))
415 ;; The customgroup should be a list of symbols.
416 (cond ((and (null customg) custom)
417 (setq customg '(default)))
418 ((not (listp customg))
419 (setq customg (list customg))))
420 ;; The customgroup better be a list of symbols.
423 (signal 'invalid-slot-type (list :group cg))))
425 ;; First up, add this slot into our new class.
427 newc (cl--make-slot-descriptor
429 `(,@(if docstr `((:documentation . ,docstr)))
430 ,@(if custom `((:custom . ,custom)))
431 ,@(if label `((:label . ,label)))
432 ,@(if customg `((:group . ,customg)))
433 ,@(if printer `((:printer . ,printer)))
434 ,@(if prot `((:protection . ,prot)))))
435 initarg alloc 'defaultoverride skip-nil)
437 ;; We need to id the group, and store them in a group list attribute.
439 (cl-pushnew cg groups :test #'equal))
442 ;; Now that everything has been loaded up, all our lists are backwards!
443 ;; Fix that up now and then them into vectors.
444 (cl-callf (lambda (slots) (apply #'vector (nreverse slots)))
445 (eieio--class-slots newc))
446 (cl-callf nreverse (eieio--class-initarg-tuples newc))
448 ;; The storage for class-class-allocation-type needs to be turned into
450 (cl-callf (lambda (slots) (apply #'vector slots))
451 (eieio--class-class-slots newc))
453 ;; Also, setup the class allocated values.
454 (let* ((slots (eieio--class-class-slots newc))
456 (v (make-vector n nil)))
458 (setf (aref v i) (eieio-default-eval-maybe
459 (cl--slot-descriptor-initform (aref slots i)))))
460 (setf (eieio--class-class-allocation-values newc) v))
462 ;; Attach slot symbols into a hashtable, and store the index of
463 ;; this slot as the value this table.
464 (let* ((slots (eieio--class-slots newc))
465 ;; (cslots (eieio--class-class-slots newc))
466 (oa (make-hash-table :test #'eq)))
467 ;; (dotimes (cnt (length cslots))
468 ;; (setf (gethash (cl--slot-descriptor-name (aref cslots cnt)) oa) (- -1 cnt)))
469 (dotimes (cnt (length slots))
470 (setf (gethash (cl--slot-descriptor-name (aref slots cnt)) oa) cnt))
471 (setf (eieio--class-index-table newc) oa))
473 ;; Set up a specialized doc string.
474 ;; Use stored value since it is calculated in a non-trivial way
475 (let ((docstring (eieio--class-option-assoc options :documentation)))
476 (setf (eieio--class-docstring newc) docstring)
477 (when eieio-backward-compatibility
478 (put cname 'variable-documentation docstring)))
480 ;; Save the file location where this class is defined.
481 (add-to-list 'current-load-list `(define-type . ,cname))
483 ;; We have a list of custom groups. Store them into the options.
484 (let ((g (eieio--class-option-assoc options :custom-groups)))
485 (mapc (lambda (cg) (cl-pushnew cg g :test 'equal)) groups)
486 (if (memq :custom-groups options)
487 (setcar (cdr (memq :custom-groups options)) g)
488 (setq options (cons :custom-groups (cons g options)))))
490 ;; Set up the options we have collected.
491 (setf (eieio--class-options newc) options)
493 ;; if this is a superclass, clear out parent (which was set to the
494 ;; default superclass eieio-default-superclass)
495 (if clearparent (setf (eieio--class-parents newc) nil))
497 ;; Create the cached default object.
498 (let ((cache (make-vector (+ (length (eieio--class-slots newc))
499 (eval-when-compile eieio--object-num-slots))
501 ;; We don't strictly speaking need to use a symbol, but the old
502 ;; code used the class's name rather than the class's object, so
503 ;; we follow this preference for using a symbol, which is probably
504 ;; convenient to keep the printed representation of such Elisp
506 (tag (intern (format "eieio-class-tag--%s" cname))))
508 (fset tag :quick-object-witness-check)
509 (setf (eieio--object-class-tag cache) tag)
510 (let ((eieio-skip-typecheck t))
511 ;; All type-checking has been done to our satisfaction
512 ;; before this call. Don't waste our time in this call..
513 (eieio-set-defaults cache t))
514 (setf (eieio--class-default-object-cache newc) cache))
516 ;; Return our new class object
521 (defsubst eieio-eval-default-p (val)
522 "Whether the default value VAL should be evaluated for use."
523 (and (consp val) (symbolp (car val)) (fboundp (car val))))
525 (defun eieio--perform-slot-validation-for-default (slot skipnil)
526 "For SLOT, signal if its type does not match its default value.
527 If SKIPNIL is non-nil, then if default value is nil return t instead."
528 (let ((value (cl--slot-descriptor-initform slot))
529 (spec (cl--slot-descriptor-type slot)))
530 (if (not (or (eieio-eval-default-p value) ;FIXME: Why?
532 (and skipnil (null value))
533 (eieio--perform-slot-validation spec value)))
534 (signal 'invalid-slot-type (list (cl--slot-descriptor-name slot) spec value)))))
536 (defun eieio--slot-override (old new skipnil)
537 (cl-assert (eq (cl--slot-descriptor-name old) (cl--slot-descriptor-name new)))
538 ;; There is a match, and we must override the old value.
539 (let* ((a (cl--slot-descriptor-name old))
540 (tp (cl--slot-descriptor-type old))
541 (d (cl--slot-descriptor-initform new))
542 (type (cl--slot-descriptor-type new))
543 (oprops (cl--slot-descriptor-props old))
544 (nprops (cl--slot-descriptor-props new))
545 (custg (alist-get :group nprops)))
546 ;; If type is passed in, is it the same?
547 (if (not (eq type t))
548 (if (not (equal type tp))
550 "Child slot type `%s' does not match inherited type `%s' for `%s'"
552 (setf (cl--slot-descriptor-type new) tp))
553 ;; If we have a repeat, only update the initarg...
554 (unless (eq d eieio-unbound)
555 (eieio--perform-slot-validation-for-default new skipnil)
556 (setf (cl--slot-descriptor-initform old) d))
558 ;; PLN Tue Jun 26 11:57:06 2007 : The protection is
559 ;; checked and SHOULD match the superclass
560 ;; protection. Otherwise an error is thrown. However
561 ;; I wonder if a more flexible schedule might be
564 ;; EML - We used to have (if prot... here,
565 ;; but a prot of 'nil means public.
567 (let ((super-prot (alist-get :protection oprops))
568 (prot (alist-get :protection nprops)))
569 (if (not (eq prot super-prot))
570 (error "Child slot protection `%s' does not match inherited protection `%s' for `%s'"
574 ;; PLN Tue Jun 26 11:57:06 2007 :
575 ;; Do a non redundant combination of ancient custom
576 ;; groups and new ones.
578 (let* ((list1 (alist-get :group oprops)))
580 (unless (memq elt list1)
582 (setf (alist-get :group (cl--slot-descriptor-props old)) list1)))
585 ;; PLN Mon Jun 25 22:44:34 2007 : If a new cust is
586 ;; set, simply replaces the old one.
587 (dolist (prop '(:custom :label :documentation :printer))
588 (when (alist-get prop (cl--slot-descriptor-props new))
589 (setf (alist-get prop (cl--slot-descriptor-props old))
590 (alist-get prop (cl--slot-descriptor-props new))))
594 (defun eieio--add-new-slot (newc slot init alloc
595 &optional defaultoverride skipnil)
596 "Add into NEWC attribute SLOT.
597 If a slot of that name already exists in NEWC, then do nothing. If it doesn't exist,
598 INIT is the initarg, if any.
599 Argument ALLOC specifies if the slot is allocated per instance, or per class.
600 If optional DEFAULTOVERRIDE is non-nil, then if A exists in NEWC,
601 we must override its value for a default.
602 Optional argument SKIPNIL indicates if type checking should be skipped
603 if default value is nil."
604 ;; Make sure we duplicate those items that are sequences.
605 (let* ((a (cl--slot-descriptor-name slot))
606 (d (cl--slot-descriptor-initform slot))
607 (old (car (cl-member a (eieio--class-slots newc)
608 :key #'cl--slot-descriptor-name)))
609 (cold (car (cl-member a (eieio--class-class-slots newc)
610 :key #'cl--slot-descriptor-name))))
611 (cl-pushnew a eieio--known-slot-names)
613 (if (sequencep d) (setq d (copy-sequence d)))
614 ;; This copy can fail on a cons cell with a non-cons in the cdr. Let's
615 ;; skip it if it doesn't work.
617 ;; (if (sequencep type) (setq type (copy-sequence type)))
618 ;; (if (sequencep cust) (setq cust (copy-sequence cust)))
619 ;; (if (sequencep custg) (setq custg (copy-sequence custg)))
621 ;; To prevent override information w/out specification of storage,
622 ;; we need to do this little hack.
623 (if cold (setq alloc :class))
625 (if (memq alloc '(nil :instance))
626 ;; In this case, we modify the INSTANCE version of a given slot.
628 ;; Only add this element if it is so-far unique
631 (eieio--perform-slot-validation-for-default slot skipnil)
632 (push slot (eieio--class-slots newc))
634 ;; When defaultoverride is true, we are usually adding new local
635 ;; attributes which must override the default value of any slot
636 ;; passed in by one of the parent classes.
637 (when defaultoverride
638 (eieio--slot-override old slot skipnil)))
640 (cl-pushnew (cons init a) (eieio--class-initarg-tuples newc)
643 ;; CLASS ALLOCATED SLOTS
646 (eieio--perform-slot-validation-for-default slot skipnil)
647 ;; Here we have found a :class version of a slot. This
648 ;; requires a very different approach.
649 (push slot (eieio--class-class-slots newc)))
650 (when defaultoverride
651 ;; There is a match, and we must override the old value.
652 (eieio--slot-override cold slot skipnil))))))
654 (defun eieio-copy-parents-into-subclass (newc)
655 "Copy into NEWC the slots of PARENTS.
656 Follow the rules of not overwriting early parents when applying to
657 the new child class."
658 (let ((sn (eieio--class-option-assoc (eieio--class-options newc)
659 :allow-nil-initform)))
660 (dolist (pcv (eieio--class-parents newc))
661 ;; First, duplicate all the slots of the parent.
662 (let ((pslots (eieio--class-slots pcv))
663 (pinit (eieio--class-initarg-tuples pcv)))
664 (dotimes (i (length pslots))
665 (let* ((sd (cl--copy-slot-descriptor (aref pslots i)))
666 (init (car (rassq (cl--slot-descriptor-name sd) pinit))))
667 (eieio--add-new-slot newc sd init nil nil sn))
669 ;; Now duplicate all the class alloc slots.
670 (let ((pcslots (eieio--class-class-slots pcv)))
671 (dotimes (i (length pcslots))
672 (eieio--add-new-slot newc (cl--copy-slot-descriptor
678 ;;; Slot type validation
680 ;; This is a hideous hack for replacing `typep' from cl-macs, to avoid
681 ;; requiring the CL library at run-time. It can be eliminated if/when
682 ;; `typep' is merged into Emacs core.
684 (defun eieio--perform-slot-validation (spec value)
685 "Return non-nil if SPEC does not match VALUE."
686 (or (eq spec t) ; t always passes
687 (eq value eieio-unbound) ; unbound always passes
688 (cl-typep value spec)))
690 (defun eieio--validate-slot-value (class slot-idx value slot)
691 "Make sure that for CLASS referencing SLOT-IDX, VALUE is valid.
692 Checks the :type specifier.
693 SLOT is the slot that is being checked, and is only used when throwing
695 (if eieio-skip-typecheck
697 ;; Trim off object IDX junk added in for the object index.
698 (setq slot-idx (- slot-idx (eval-when-compile eieio--object-num-slots)))
699 (let ((st (cl--slot-descriptor-type (aref (eieio--class-slots class)
701 (if (not (eieio--perform-slot-validation st value))
702 (signal 'invalid-slot-type
703 (list (eieio--class-name class) slot st value))))))
705 (defun eieio--validate-class-slot-value (class slot-idx value slot)
706 "Make sure that for CLASS referencing SLOT-IDX, VALUE is valid.
707 Checks the :type specifier.
708 SLOT is the slot that is being checked, and is only used when throwing
710 (if eieio-skip-typecheck
712 (let ((st (cl--slot-descriptor-type (aref (eieio--class-class-slots class)
714 (if (not (eieio--perform-slot-validation st value))
715 (signal 'invalid-slot-type
716 (list (eieio--class-name class) slot st value))))))
718 (defun eieio-barf-if-slot-unbound (value instance slotname fn)
719 "Throw a signal if VALUE is a representation of an UNBOUND slot.
720 INSTANCE is the object being referenced. SLOTNAME is the offending
721 slot. If the slot is ok, return VALUE.
722 Argument FN is the function calling this verifier."
723 (if (and (eq value eieio-unbound) (not eieio-skip-typecheck))
724 (slot-unbound instance (eieio--object-class instance) slotname fn)
728 ;;; Get/Set slots in an object.
730 (defun eieio-oref (obj slot)
731 "Return the value in OBJ at SLOT in the object vector."
732 (declare (compiler-macro
736 ((and (or `',name (and name (pred keywordp)))
737 (guard (not (memq name eieio--known-slot-names))))
738 (macroexp--warn-and-return
739 (format-message "Unknown slot `%S'" name) exp 'compile-only))
741 (cl-check-type slot symbol)
742 (cl-check-type obj (or eieio-object class))
743 (let* ((class (cond ((symbolp obj)
744 (error "eieio-oref called on a class: %s" obj)
745 (let ((c (cl--find-class obj)))
746 (if (eieio--class-p c) (eieio-class-un-autoload obj))
748 (t (eieio--object-class obj))))
749 (c (eieio--slot-name-index class slot)))
751 ;; It might be missing because it is a :class allocated slot.
752 ;; Let's check that info out.
753 (if (setq c (eieio--class-slot-name-index class slot))
755 (aref (eieio--class-class-allocation-values class) c)
756 ;; The slot-missing method is a cool way of allowing an object author
757 ;; to intercept missing slot definitions. Since it is also the LAST
758 ;; thing called in this fn, its return value would be retrieved.
759 (slot-missing obj slot 'oref)
760 ;;(signal 'invalid-slot-name (list (eieio-object-name obj) slot))
762 (cl-check-type obj eieio-object)
763 (eieio-barf-if-slot-unbound (aref obj c) obj slot 'oref))))
766 (defun eieio-oref-default (obj slot)
767 "Do the work for the macro `oref-default' with similar parameters.
768 Fills in OBJ's SLOT with its default value."
769 (cl-check-type obj (or eieio-object class))
770 (cl-check-type slot symbol)
771 (let* ((cl (cond ((symbolp obj) (cl--find-class obj))
772 (t (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 ;;(signal 'invalid-slot-name (list (class-name cl) slot))
785 (eieio-barf-if-slot-unbound
786 (let ((val (cl--slot-descriptor-initform
787 (aref (eieio--class-slots cl)
788 (- c (eval-when-compile eieio--object-num-slots))))))
789 (eieio-default-eval-maybe val))
790 obj (eieio--class-name cl) 'oref-default))))
792 (defun eieio-default-eval-maybe (val)
793 "Check VAL, and return what `oref-default' would provide."
794 ;; FIXME: What the hell is this supposed to do? Shouldn't it evaluate
795 ;; variables as well? Why not just always call `eval'?
797 ;; Is it a function call? If so, evaluate it.
798 ((eieio-eval-default-p val)
800 ;;;; check for quoted things, and unquote them
801 ;;((and (consp val) (eq (car val) 'quote))
803 ;; return it verbatim
806 (defun eieio-oset (obj slot value)
807 "Do the work for the macro `oset'.
808 Fills in OBJ's SLOT with VALUE."
809 (cl-check-type obj eieio-object)
810 (cl-check-type slot symbol)
811 (let* ((class (eieio--object-class obj))
812 (c (eieio--slot-name-index class slot)))
814 ;; It might be missing because it is a :class allocated slot.
815 ;; Let's check that info out.
817 (eieio--class-slot-name-index class slot))
820 (eieio--validate-class-slot-value class c value slot)
821 (aset (eieio--class-class-allocation-values class)
823 ;; See oref for comment on `slot-missing'
824 (slot-missing obj slot 'oset value)
825 ;;(signal 'invalid-slot-name (list (eieio-object-name obj) slot))
827 (eieio--validate-slot-value class c value slot)
828 (aset obj c value))))
830 (defun eieio-oset-default (class slot value)
831 "Do the work for the macro `oset-default'.
832 Fills in the default value in CLASS' in SLOT with VALUE."
833 (setq class (eieio--class-object class))
834 (cl-check-type class eieio--class)
835 (cl-check-type slot symbol)
836 (let* ((c (eieio--slot-name-index class slot)))
838 ;; It might be missing because it is a :class allocated slot.
839 ;; Let's check that info out.
840 (if (setq c (eieio--class-slot-name-index class slot))
843 (eieio--validate-class-slot-value class c value slot)
844 (aset (eieio--class-class-allocation-values class) c
846 (signal 'invalid-slot-name (list (eieio--class-name class) slot)))
847 ;; `oset-default' on an instance-allocated slot is allowed by EIEIO but
848 ;; not by CLOS and is mildly inconsistent with the :initform thingy, so
849 ;; it'd be nice to get of it. This said, it is/was used at one place by
850 ;; gnus/registry.el, so it might be used elsewhere as well, so let's
852 ;; FIXME: Generate a compile-time warning for it!
853 ;; (error "Can't `oset-default' an instance-allocated slot: %S of %S"
855 (eieio--validate-slot-value class c value slot)
856 ;; Set this into the storage for defaults.
857 (if (eieio-eval-default-p value)
858 (error "Can't set default to a sexp that gets evaluated again"))
859 (setf (cl--slot-descriptor-initform
860 ;; FIXME: Apparently we set it both in `slots' and in
861 ;; `object-cache', which seems redundant.
862 (aref (eieio--class-slots class)
863 (- c (eval-when-compile eieio--object-num-slots))))
865 ;; Take the value, and put it into our cache object.
866 (eieio-oset (eieio--class-default-object-cache class)
871 ;;; EIEIO internal search functions
873 (defun eieio--slot-name-index (class slot)
874 "In CLASS find the index of the named SLOT.
875 The slot is a symbol which is installed in CLASS by the `defclass' call.
876 If SLOT is the value created with :initarg instead,
877 reverse-lookup that name, and recurse with the associated slot value."
878 ;; Removed checks to outside this call
879 (let* ((fsi (gethash slot (eieio--class-index-table class))))
881 (+ (eval-when-compile eieio--object-num-slots) fsi)
882 (let ((fn (eieio--initarg-to-attribute class slot)))
884 ;; Accessing a slot via its :initarg is accepted by EIEIO
885 ;; (but not CLOS) but is a bad idea (for one: it's slower).
886 ;; FIXME: We should emit a compile-time warning when this happens!
887 (eieio--slot-name-index class fn)
890 (defun eieio--class-slot-name-index (class slot)
891 "In CLASS find the index of the named SLOT.
892 The slot is a symbol which is installed in CLASS by the `defclass'
893 call. If SLOT is the value created with :initarg instead,
894 reverse-lookup that name, and recurse with the associated slot value."
895 ;; This will happen less often, and with fewer slots. Do this the
896 ;; storage cheap way.
898 (slots (eieio--class-class-slots class)))
899 (dotimes (i (length slots))
900 (if (eq slot (cl--slot-descriptor-name (aref slots i)))
905 ;; Way to assign slots based on a list. Used for constructors, or
906 ;; even resetting an object at run-time
908 (defun eieio-set-defaults (obj &optional set-all)
909 "Take object OBJ, and reset all slots to their defaults.
910 If SET-ALL is non-nil, then when a default is nil, that value is
911 reset. If SET-ALL is nil, the slots are only reset if the default is
913 (let ((slots (eieio--class-slots (eieio--object-class obj))))
914 (dotimes (i (length slots))
915 (let* ((name (cl--slot-descriptor-name (aref slots i)))
916 (df (eieio-oref-default obj name)))
918 (eieio-oset obj name df))))))
920 (defun eieio--initarg-to-attribute (class initarg)
921 "For CLASS, convert INITARG to the actual attribute name.
922 If there is no translation, pass it in directly (so we can cheat if
923 need be... May remove that later...)"
924 (let ((tuple (assoc initarg (eieio--class-initarg-tuples class))))
930 ;; Method Invocation order: C3
931 (defun eieio--c3-candidate (class remaining-inputs)
932 "Return CLASS if it can go in the result now, otherwise nil."
933 ;; Ensure CLASS is not in any position but the first in any of the
934 ;; element lists of REMAINING-INPUTS.
935 (and (not (let ((found nil))
936 (while (and remaining-inputs (not found))
937 (setq found (member class (cdr (car remaining-inputs)))
938 remaining-inputs (cdr remaining-inputs)))
942 (defun eieio--c3-merge-lists (reversed-partial-result remaining-inputs)
943 "Merge REVERSED-PARTIAL-RESULT REMAINING-INPUTS in a consistent order, if possible.
944 If a consistent order does not exist, signal an error."
945 (setq remaining-inputs (delq nil remaining-inputs))
946 (if (null remaining-inputs)
947 ;; If all remaining inputs are empty lists, we are done.
948 (nreverse reversed-partial-result)
949 ;; Otherwise, we try to find the next element of the result. This
950 ;; is achieved by considering the first element of each
951 ;; (non-empty) input list and accepting a candidate if it is
952 ;; consistent with the rests of the input lists.
954 (tail remaining-inputs)
956 (while (and tail (not found))
957 (setq found (eieio--c3-candidate (caar tail)
962 ;; The graph is consistent so far, add NEXT to result and
963 ;; merge input lists, dropping NEXT from their heads where
965 (eieio--c3-merge-lists
966 (cons next reversed-partial-result)
967 (mapcar (lambda (l) (if (eq (cl-first l) next) (cl-rest l) l))
969 ;; The graph is inconsistent, give up
970 (signal 'inconsistent-class-hierarchy (list remaining-inputs))))))
972 (defsubst eieio--class/struct-parents (class)
973 (or (eieio--class-parents class)
974 `(,eieio-default-superclass)))
976 (defun eieio--class-precedence-c3 (class)
977 "Return all parents of CLASS in c3 order."
978 (let ((parents (eieio--class-parents (cl--find-class class))))
979 (eieio--c3-merge-lists
983 (mapcar #'eieio--class-precedence-c3 parents)
984 `((,eieio-default-superclass)))
988 ;; Method Invocation Order: Depth First
990 (defun eieio--class-precedence-dfs (class)
991 "Return all parents of CLASS in depth-first order."
992 (let* ((parents (eieio--class-parents class))
993 (classes (copy-sequence
1000 (eieio--class-precedence-dfs parent)))
1002 `((,eieio-default-superclass))))))
1004 ;; Remove duplicates.
1006 (setcdr tail (delq (car tail) (cdr tail)))
1007 (setq tail (cdr tail)))
1011 ;; Method Invocation Order: Breadth First
1012 (defun eieio--class-precedence-bfs (class)
1013 "Return all parents of CLASS in breadth-first order."
1015 (queue (eieio--class/struct-parents class)))
1017 (let ((head (pop queue)))
1018 (unless (member head result)
1020 (unless (eq head eieio-default-superclass)
1021 (setq queue (append queue (eieio--class/struct-parents head)))))))
1022 (cons class (nreverse result)))
1026 ;; Method Invocation Order
1028 (defun eieio--class-precedence-list (class)
1029 "Return (transitively closed) list of parents of CLASS.
1030 The order, in which the parents are returned depends on the
1031 method invocation orders of the involved classes."
1032 (if (or (null class) (eq class eieio-default-superclass))
1034 (unless (eieio--class-default-object-cache class)
1035 (eieio-class-un-autoload (eieio--class-name class)))
1036 (cl-case (eieio--class-method-invocation-order class)
1038 (eieio--class-precedence-dfs class))
1040 (eieio--class-precedence-bfs class))
1042 (eieio--class-precedence-c3 class))))
1044 (define-obsolete-function-alias
1045 'class-precedence-list 'eieio--class-precedence-list "24.4")
1048 ;;; Here are some special types of errors
1050 (define-error 'invalid-slot-name "Invalid slot name")
1051 (define-error 'invalid-slot-type "Invalid slot type")
1052 (define-error 'unbound-slot "Unbound slot")
1053 (define-error 'inconsistent-class-hierarchy "Inconsistent class hierarchy")
1055 ;;; Hooking into cl-generic.
1057 (require 'cl-generic)
1059 ;;;; General support to dispatch based on the type of the argument.
1061 (defconst eieio--generic-generalizer
1062 (cl-generic-make-generalizer
1063 ;; Use the exact same tagcode as for cl-struct, so that methods
1064 ;; that dispatch on both kinds of objects get to share this
1065 ;; part of the dispatch code.
1066 50 #'cl--generic-struct-tag
1068 (and (symbolp tag) (boundp tag) (eieio--class-p (symbol-value tag))
1069 (mapcar #'eieio--class-name
1070 (eieio--class-precedence-list (symbol-value tag)))))))
1072 (cl-defmethod cl-generic-generalizers :extra "class" (specializer)
1074 ;; A class must be defined before it can be used as a parameter
1075 ;; specializer in a defmethod form.
1076 ;; So we can ignore types that are not known to denote classes.
1078 (and (eieio--class-p (eieio--class-object specializer))
1079 (list eieio--generic-generalizer))
1080 (cl-call-next-method)))
1082 ;;;; Dispatch for arguments which are classes.
1084 ;; Since EIEIO does not support metaclasses, users can't easily use the
1085 ;; "dispatch on argument type" for class arguments. That's why EIEIO's
1086 ;; `defmethod' added the :static qualifier. For cl-generic, such a qualifier
1087 ;; would not make much sense (e.g. to which argument should it apply?).
1088 ;; Instead, we add a new "subclass" specializer.
1090 (defun eieio--generic-subclass-specializers (tag)
1091 (when (eieio--class-p tag)
1092 (mapcar (lambda (class)
1093 `(subclass ,(eieio--class-name class)))
1094 (eieio--class-precedence-list tag))))
1096 (defconst eieio--generic-subclass-generalizer
1097 (cl-generic-make-generalizer
1098 60 (lambda (name) `(and (symbolp ,name) (cl--find-class ,name)))
1099 #'eieio--generic-subclass-specializers))
1101 (cl-defmethod cl-generic-generalizers ((_specializer (head subclass)))
1102 (list eieio--generic-subclass-generalizer))
1105 ;;;### (autoloads nil "eieio-compat" "eieio-compat.el" "ea8c7f24ed47c6b71ac37cbdae1c9931")
1106 ;;; Generated autoloads from eieio-compat.el
1108 (autoload 'eieio--defalias "eieio-compat" "\
1109 Like `defalias', but with less side-effects.
1110 More specifically, it has no side-effects at all when the new function
1111 definition is the same (`eq') as the old one.
1113 \(fn NAME BODY)" nil nil)
1115 (autoload 'defgeneric "eieio-compat" "\
1116 Create a generic function METHOD.
1117 DOC-STRING is the base documentation for this class. A generic
1118 function has no body, as its purpose is to decide which method body
1119 is appropriate to use. Uses `defmethod' to create methods, and calls
1120 `defgeneric' for you. With this implementation the ARGS are
1121 currently ignored. You can use `defgeneric' to apply specialized
1122 top level documentation to a method.
1124 \(fn METHOD ARGS &optional DOC-STRING)" nil t)
1126 (function-put 'defgeneric 'doc-string-elt '3)
1128 (make-obsolete 'defgeneric 'cl-defgeneric '"25.1")
1130 (autoload 'defmethod "eieio-compat" "\
1131 Create a new METHOD through `defgeneric' with ARGS.
1133 The optional second argument KEY is a specifier that
1134 modifies how the method is called, including:
1135 :before - Method will be called before the :primary
1136 :primary - The default if not specified
1137 :after - Method will be called after the :primary
1138 :static - First arg could be an object or class
1139 The next argument is the ARGLIST. The ARGLIST specifies the arguments
1140 to the method as with `defun'. The first argument can have a type
1142 ((VARNAME CLASS) ARG2 ...)
1143 where VARNAME is the name of the local variable for the method being
1144 created. The CLASS is a class symbol for a class made with `defclass'.
1145 A DOCSTRING comes after the ARGLIST, and is optional.
1146 All the rest of the args are the BODY of the method. A method will
1147 return the value of the last form in the BODY.
1151 (defmethod mymethod [:before | :primary | :after | :static]
1152 ((typearg class-name) arg2 &optional opt &rest rest)
1156 \(fn METHOD &rest ARGS)" nil t)
1158 (function-put 'defmethod 'doc-string-elt '3)
1160 (make-obsolete 'defmethod 'cl-defmethod '"25.1")
1162 (autoload 'eieio--defgeneric-init-form "eieio-compat" "\
1165 \(fn METHOD DOC-STRING)" nil nil)
1167 (autoload 'eieio--defmethod "eieio-compat" "\
1170 \(fn METHOD KIND ARGCLASS CODE)" nil nil)
1172 (autoload 'eieio-defmethod "eieio-compat" "\
1173 Obsolete work part of an old version of the `defmethod' macro.
1175 \(fn METHOD ARGS)" nil nil)
1177 (make-obsolete 'eieio-defmethod 'cl-defmethod '"24.1")
1179 (autoload 'eieio-defgeneric "eieio-compat" "\
1180 Obsolete work part of an old version of the `defgeneric' macro.
1182 \(fn METHOD DOC-STRING)" nil nil)
1184 (make-obsolete 'eieio-defgeneric 'cl-defgeneric '"24.1")
1186 (autoload 'eieio-defclass "eieio-compat" "\
1189 \(fn CNAME SUPERCLASSES SLOTS OPTIONS)" nil nil)
1191 (make-obsolete 'eieio-defclass 'eieio-defclass-internal '"25.1")
1196 (provide 'eieio-core)
1198 ;;; eieio-core.el ends here