emacs-lisp/package.el and package-x.el: References to package-desc-kind
[emacs.git] / lisp / emacs-lisp / eieio-core.el
blobbfa922bade6e0941646f7750166667b8528445a4
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>
6 ;; Version: 1.4
7 ;; Keywords: OO, lisp
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/>.
24 ;;; Commentary:
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.
32 ;;; Code:
34 (require 'cl-lib)
35 (require 'pcase)
37 (put 'eieio--defalias 'byte-hunk-handler
38 #'byte-compile-file-form-defalias) ;;(get 'defalias 'byte-hunk-handler)
39 (defun eieio--defalias (name body)
40 "Like `defalias', but with less side-effects.
41 More specifically, it has no side-effects at all when the new function
42 definition is the same (`eq') as the old one."
43 (while (and (fboundp name) (symbolp (symbol-function name)))
44 ;; Follow aliases, so methods applied to obsolete aliases still work.
45 (setq name (symbol-function name)))
46 (unless (and (fboundp name)
47 (eq (symbol-function name) body))
48 (defalias name body)))
50 ;;;
51 ;; A few functions that are better in the official EIEIO src, but
52 ;; used from the core.
53 (declare-function slot-unbound "eieio")
54 (declare-function slot-missing "eieio")
55 (declare-function child-of-class-p "eieio")
58 ;;;
59 ;; Variable declarations.
61 (defvar eieio-hook nil
62 "This hook is executed, then cleared each time `defclass' is called.")
64 (defvar eieio-error-unsupported-class-tags nil
65 "Non-nil to throw an error if an encountered tag is unsupported.
66 This may prevent classes from CLOS applications from being used with EIEIO
67 since EIEIO does not support all CLOS tags.")
69 (defvar eieio-skip-typecheck nil
70 "If non-nil, skip all slot typechecking.
71 Set this to t permanently if a program is functioning well to get a
72 small speed increase. This variable is also used internally to handle
73 default setting for optimization purposes.")
75 (defvar eieio-optimize-primary-methods-flag t
76 "Non-nil means to optimize the method dispatch on primary methods.")
78 (defvar eieio-initializing-object nil
79 "Set to non-nil while initializing an object.")
81 (defvar eieio-backward-compatibility t
82 "If nil, drop support for some behaviors of older versions of EIEIO.
83 Currently under control of this var:
84 - Define every class as a var whose value is the class symbol.
85 - Define <class>-child-p and <class>-list-p predicates.
86 - Allow object names in constructors.")
88 (defconst eieio-unbound
89 (if (and (boundp 'eieio-unbound) (symbolp eieio-unbound))
90 eieio-unbound
91 (make-symbol "unbound"))
92 "Uninterned symbol representing an unbound slot in an object.")
94 ;; This is a bootstrap for eieio-default-superclass so it has a value
95 ;; while it is being built itself.
96 (defvar eieio-default-superclass nil)
98 ;;;
99 ;; Class currently in scope.
101 ;; When invoking methods, the running method needs to know which class
102 ;; is currently in scope. Generally this is the class of the method
103 ;; being called, but 'call-next-method' needs to query this state,
104 ;; and change it to be then next super class up.
106 ;; Thus, the scoped class is a stack that needs to be managed.
108 (defvar eieio--scoped-class-stack nil
109 "A stack of the classes currently in scope during method invocation.")
111 (defun eieio--scoped-class ()
112 "Return the class object currently in scope, or nil."
113 (car-safe eieio--scoped-class-stack))
115 (defmacro eieio--with-scoped-class (class &rest forms)
116 "Set CLASS as the currently scoped class while executing FORMS."
117 (declare (indent 1))
118 `(let ((eieio--scoped-class-stack (cons ,class eieio--scoped-class-stack)))
119 ,@forms))
121 (progn
122 ;; Arrange for field access not to bother checking if the access is indeed
123 ;; made to an eieio--class object.
124 (cl-declaim (optimize (safety 0)))
125 (cl-defstruct (eieio--class
126 (:constructor nil)
127 (:constructor eieio--class-make (symbol &aux (tag 'defclass)))
128 (:type vector)
129 (:copier nil))
130 ;; We use an untagged cl-struct, with our own hand-made tag as first field
131 ;; (containing the symbol `defclass'). It would be better to use a normal
132 ;; cl-struct with its normal tag (e.g. so that cl-defstruct can define the
133 ;; predicate for us), but that breaks compatibility with .elc files compiled
134 ;; against older versions of EIEIO.
136 symbol ;; symbol (self-referencing)
137 parent children
138 symbol-hashtable ;; hashtable permitting fast access to variable position indexes
139 ;; @todo
140 ;; the word "public" here is leftovers from the very first version.
141 ;; Get rid of it!
142 public-a ;; class attribute index
143 public-d ;; class attribute defaults index
144 public-doc ;; class documentation strings for attributes
145 public-type ;; class type for a slot
146 public-custom ;; class custom type for a slot
147 public-custom-label ;; class custom group for a slot
148 public-custom-group ;; class custom group for a slot
149 public-printer ;; printer for a slot
150 protection ;; protection for a slot
151 initarg-tuples ;; initarg tuples list
152 class-allocation-a ;; class allocated attributes
153 class-allocation-doc ;; class allocated documentation
154 class-allocation-type ;; class allocated value type
155 class-allocation-custom ;; class allocated custom descriptor
156 class-allocation-custom-label ;; class allocated custom descriptor
157 class-allocation-custom-group ;; class allocated custom group
158 class-allocation-printer ;; class allocated printer for a slot
159 class-allocation-protection ;; class allocated protection list
160 class-allocation-values ;; class allocated value vector
161 default-object-cache ;; what a newly created object would look like.
162 ; This will speed up instantiation time as
163 ; only a `copy-sequence' will be needed, instead of
164 ; looping over all the values and setting them from
165 ; the default.
166 options ;; storage location of tagged class option
167 ; Stored outright without modifications or stripping
169 ;; Set it back to the default value.
170 (cl-declaim (optimize (safety 1))))
173 (cl-defstruct (eieio--object
174 (:type vector) ;We manage our own tagging system.
175 (:constructor nil)
176 (:copier nil))
177 ;; `class-tag' holds a symbol, which is not the class name, but is instead
178 ;; properly prefixed as an internal EIEIO thingy and which holds the class
179 ;; object/struct in its `symbol-value' slot.
180 class-tag)
182 (eval-and-compile
183 (defconst eieio--object-num-slots
184 (length (get 'eieio--object 'cl-struct-slots))))
186 (defsubst eieio--object-class-object (obj)
187 (symbol-value (eieio--object-class-tag obj)))
189 (defsubst eieio--object-class-name (obj)
190 ;; FIXME: Most uses of this function should be changed to use
191 ;; eieio--object-class-object instead!
192 (eieio--class-symbol (eieio--object-class-object obj)))
195 ;;; Important macros used internally in eieio.
197 (defmacro eieio--check-type (type obj)
198 (unless (symbolp obj)
199 (error "eieio--check-type wants OBJ to be a variable"))
200 `(if (not ,(cond
201 ((eq 'or (car-safe type))
202 `(or ,@(mapcar (lambda (type) `(,type ,obj)) (cdr type))))
203 (t `(,type ,obj))))
204 (signal 'wrong-type-argument (list ',type ,obj))))
206 (defmacro eieio--class-v (class) ;Use a macro, so it acts as a GV place.
207 "Internal: Return the class vector from the CLASS symbol."
208 (declare (debug t))
209 ;; No check: If eieio gets this far, it has probably been checked already.
210 `(get ,class 'eieio-class-definition))
212 (defsubst eieio--class-object (class)
213 "Return the class object."
214 (if (symbolp class)
215 ;; Keep the symbol if class-v is nil, for better error messages.
216 (or (eieio--class-v class) class)
217 class))
219 (defsubst eieio--class-p (class)
220 "Return non-nil if CLASS is a valid class object."
221 (condition-case nil
222 (eq (aref class 0) 'defclass)
223 (error nil)))
225 (defsubst eieio-class-object (class)
226 "Check that CLASS is a class and return the corresponding object."
227 (let ((c (eieio--class-object class)))
228 (eieio--check-type eieio--class-p c)
231 (defsubst class-p (class)
232 "Return non-nil if CLASS is a valid class vector.
233 CLASS is a symbol." ;FIXME: Is it a vector or a symbol?
234 ;; this new method is faster since it doesn't waste time checking lots of
235 ;; things.
236 (condition-case nil
237 (eq (aref (eieio--class-v class) 0) 'defclass)
238 (error nil)))
240 (defun eieio-class-name (class)
241 "Return a Lisp like symbol name for CLASS."
242 ;; FIXME: What's a "Lisp like symbol name"?
243 ;; FIXME: CLOS returns a symbol, but the code returns a string.
244 (if (eieio--class-p class) (setq class (eieio--class-symbol class)))
245 (eieio--check-type class-p class)
246 ;; I think this is supposed to return a symbol, but to me CLASS is a symbol,
247 ;; and I wanted a string. Arg!
248 (format "#<class %s>" (symbol-name class)))
249 (define-obsolete-function-alias 'class-name #'eieio-class-name "24.4")
251 (defmacro class-constructor (class)
252 "Return the symbol representing the constructor of CLASS."
253 (declare (debug t))
254 `(eieio--class-symbol (eieio--class-v ,class)))
256 (defmacro eieio--class-option-assoc (list option)
257 "Return from LIST the found OPTION, or nil if it doesn't exist."
258 `(car-safe (cdr (memq ,option ,list))))
260 (defsubst eieio--class-option (class option)
261 "Return the value stored for CLASS' OPTION.
262 Return nil if that option doesn't exist."
263 (eieio--class-option-assoc (eieio--class-options class) option))
265 (defsubst eieio-object-p (obj)
266 "Return non-nil if OBJ is an EIEIO object."
267 (and (vectorp obj)
268 (condition-case nil
269 (eq (aref (eieio--object-class-object obj) 0) 'defclass)
270 (error nil))))
272 (defalias 'object-p 'eieio-object-p)
274 (defsubst class-abstract-p (class)
275 "Return non-nil if CLASS is abstract.
276 Abstract classes cannot be instantiated."
277 (eieio--class-option (eieio--class-v class) :abstract))
279 (defsubst eieio--class-method-invocation-order (class)
280 "Return the invocation order of CLASS.
281 Abstract classes cannot be instantiated."
282 (or (eieio--class-option class :method-invocation-order)
283 :breadth-first))
288 ;; Class Creation
290 (defvar eieio-defclass-autoload-map (make-hash-table)
291 "Symbol map of superclasses we find in autoloads.")
293 ;; We autoload this because it's used in `make-autoload'.
294 ;;;###autoload
295 (defun eieio-defclass-autoload (cname superclasses filename doc)
296 "Create autoload symbols for the EIEIO class CNAME.
297 SUPERCLASSES are the superclasses that CNAME inherits from.
298 DOC is the docstring for CNAME.
299 This function creates a mock-class for CNAME and adds it into
300 SUPERCLASSES as children.
301 It creates an autoload function for CNAME's constructor."
302 ;; Assume we've already debugged inputs.
304 (let* ((oldc (when (class-p cname) (eieio--class-v cname)))
305 (newc (eieio--class-make cname))
307 (if oldc
308 nil ;; Do nothing if we already have this class.
310 (let ((clear-parent nil))
311 ;; No parents?
312 (when (not superclasses)
313 (setq superclasses '(eieio-default-superclass)
314 clear-parent t)
317 ;; Hook our new class into the existing structures so we can
318 ;; autoload it later.
319 (dolist (SC superclasses)
322 ;; TODO - If we create an autoload that is in the map, that
323 ;; map needs to be cleared!
326 ;; Save the child in the parent.
327 (cl-pushnew cname (if (class-p SC)
328 (eieio--class-children (eieio--class-v SC))
329 ;; Parent doesn't exist yet.
330 (gethash SC eieio-defclass-autoload-map)))
332 ;; Save parent in child.
333 (push (eieio--class-v SC) (eieio--class-parent newc)))
335 ;; turn this into a usable self-pointing symbol
336 (when eieio-backward-compatibility
337 (set cname cname)
338 (make-obsolete-variable cname (format "use '%s instead" cname) "25.1"))
340 ;; Store the new class vector definition into the symbol. We need to
341 ;; do this first so that we can call defmethod for the accessor.
342 ;; The vector will be updated by the following while loop and will not
343 ;; need to be stored a second time.
344 (setf (eieio--class-v cname) newc)
346 ;; Clear the parent
347 (if clear-parent (setf (eieio--class-parent newc) nil))
349 ;; Create an autoload on top of our constructor function.
350 (autoload cname filename doc nil nil)
351 (autoload (intern (concat (symbol-name cname) "-p")) filename "" nil nil)
352 (autoload (intern (concat (symbol-name cname) "-child-p")) filename "" nil nil)
353 (autoload (intern (concat (symbol-name cname) "-list-p")) filename "" nil nil)
355 ))))
357 (defsubst eieio-class-un-autoload (cname)
358 "If class CNAME is in an autoload state, load its file."
359 (autoload-do-load (symbol-function cname))) ; cname
361 (cl-deftype list-of (elem-type)
362 `(and list
363 (satisfies (lambda (list)
364 (cl-every (lambda (elem) (cl-typep elem ',elem-type))
365 list)))))
367 (declare-function eieio--defmethod "eieio-generic" (method kind argclass code))
369 (defun eieio-defclass-internal (cname superclasses slots options)
370 "Define CNAME as a new subclass of SUPERCLASSES.
371 SLOTS are the slots residing in that class definition, and OPTIONS
372 holds the class options.
373 See `defclass' for more information."
374 ;; Run our eieio-hook each time, and clear it when we are done.
375 ;; This way people can add hooks safely if they want to modify eieio
376 ;; or add definitions when eieio is loaded or something like that.
377 (run-hooks 'eieio-hook)
378 (setq eieio-hook nil)
380 (let* ((pname superclasses)
381 (newc (eieio--class-make cname))
382 (oldc (when (class-p cname) (eieio--class-v cname)))
383 (groups nil) ;; list of groups id'd from slots
384 (clearparent nil))
386 ;; If this class already existed, and we are updating its structure,
387 ;; make sure we keep the old child list. This can cause bugs, but
388 ;; if no new slots are created, it also saves time, and prevents
389 ;; method table breakage, particularly when the users is only
390 ;; byte compiling an EIEIO file.
391 (if oldc
392 (setf (eieio--class-children newc) (eieio--class-children oldc))
393 ;; If the old class did not exist, but did exist in the autoload map,
394 ;; then adopt those children. This is like the above, but deals with
395 ;; autoloads nicely.
396 (let ((children (gethash cname eieio-defclass-autoload-map)))
397 (when children
398 (setf (eieio--class-children newc) children)
399 (remhash cname eieio-defclass-autoload-map))))
401 (if pname
402 (progn
403 (dolist (p pname)
404 (if (and p (symbolp p))
405 (if (not (class-p p))
406 ;; bad class
407 (error "Given parent class %S is not a class" p)
408 ;; good parent class...
409 ;; save new child in parent
410 (cl-pushnew cname (eieio--class-children (eieio--class-v p)))
411 ;; Get custom groups, and store them into our local copy.
412 (mapc (lambda (g) (cl-pushnew g groups :test #'equal))
413 (eieio--class-option (eieio--class-v p) :custom-groups))
414 ;; save parent in child
415 (push (eieio--class-v p) (eieio--class-parent newc)))
416 (error "Invalid parent class %S" p)))
417 ;; Reverse the list of our parents so that they are prioritized in
418 ;; the same order as specified in the code.
419 (cl-callf nreverse (eieio--class-parent newc)))
420 ;; If there is nothing to loop over, then inherit from the
421 ;; default superclass.
422 (unless (eq cname 'eieio-default-superclass)
423 ;; adopt the default parent here, but clear it later...
424 (setq clearparent t)
425 ;; save new child in parent
426 (cl-pushnew cname (eieio--class-children eieio-default-superclass))
427 ;; save parent in child
428 (setf (eieio--class-parent newc) (list eieio-default-superclass))))
430 ;; turn this into a usable self-pointing symbol; FIXME: Why?
431 (when eieio-backward-compatibility
432 (set cname cname)
433 (make-obsolete-variable cname (format "use '%s instead" cname) "25.1"))
435 ;; Create a handy list of the class test too
436 (when eieio-backward-compatibility
437 (let ((csym (intern (concat (symbol-name cname) "-list-p"))))
438 (defalias csym
439 `(lambda (obj)
440 ,(format
441 "Test OBJ to see if it a list of objects which are a child of type %s"
442 cname)
443 (when (listp obj)
444 (let ((ans t)) ;; nil is valid
445 ;; Loop over all the elements of the input list, test
446 ;; each to make sure it is a child of the desired object class.
447 (while (and obj ans)
448 (setq ans (and (eieio-object-p (car obj))
449 (object-of-class-p (car obj) ,cname)))
450 (setq obj (cdr obj)))
451 ans))))
452 (make-obsolete csym (format "use (cl-typep ... '(list-of %s)) instead"
453 cname)
454 "25.1")))
456 ;; Before adding new slots, let's add all the methods and classes
457 ;; in from the parent class.
458 (eieio-copy-parents-into-subclass newc superclasses)
460 ;; Store the new class vector definition into the symbol. We need to
461 ;; do this first so that we can call defmethod for the accessor.
462 ;; The vector will be updated by the following while loop and will not
463 ;; need to be stored a second time.
464 (setf (eieio--class-v cname) newc)
466 ;; Query each slot in the declaration list and mangle into the
467 ;; class structure I have defined.
468 (pcase-dolist (`(,name . ,slot) slots)
469 (let* ((init (or (plist-get slot :initform)
470 (if (member :initform slot) nil
471 eieio-unbound)))
472 (initarg (plist-get slot :initarg))
473 (docstr (plist-get slot :documentation))
474 (prot (plist-get slot :protection))
475 (alloc (plist-get slot :allocation))
476 (type (plist-get slot :type))
477 (custom (plist-get slot :custom))
478 (label (plist-get slot :label))
479 (customg (plist-get slot :group))
480 (printer (plist-get slot :printer))
482 (skip-nil (eieio--class-option-assoc options :allow-nil-initform))
485 ;; Clean up the meaning of protection.
486 (setq prot
487 (pcase prot
488 ((or 'nil 'public ':public) nil)
489 ((or 'protected ':protected) 'protected)
490 ((or 'private ':private) 'private)
491 (_ (signal 'invalid-slot-type (list :protection prot)))))
493 ;; The default type specifier is supposed to be t, meaning anything.
494 (if (not type) (setq type t))
496 ;; intern the symbol so we can use it blankly
497 (if eieio-backward-compatibility
498 (and initarg (not (keywordp initarg))
499 (progn
500 (set initarg initarg)
501 (make-obsolete-variable
502 initarg (format "use '%s instead" initarg) "25.1"))))
504 ;; The customgroup should be a list of symbols
505 (cond ((null customg)
506 (setq customg '(default)))
507 ((not (listp customg))
508 (setq customg (list customg))))
509 ;; The customgroup better be a symbol, or list of symbols.
510 (mapc (lambda (cg)
511 (if (not (symbolp cg))
512 (signal 'invalid-slot-type (list :group cg))))
513 customg)
515 ;; First up, add this slot into our new class.
516 (eieio--add-new-slot newc name init docstr type custom label customg printer
517 prot initarg alloc 'defaultoverride skip-nil)
519 ;; We need to id the group, and store them in a group list attribute.
520 (dolist (cg customg)
521 (cl-pushnew cg groups :test 'equal))
524 ;; Now that everything has been loaded up, all our lists are backwards!
525 ;; Fix that up now.
526 (cl-callf nreverse (eieio--class-public-a newc))
527 (cl-callf nreverse (eieio--class-public-d newc))
528 (cl-callf nreverse (eieio--class-public-doc newc))
529 (cl-callf (lambda (types) (apply #'vector (nreverse types)))
530 (eieio--class-public-type newc))
531 (cl-callf nreverse (eieio--class-public-custom newc))
532 (cl-callf nreverse (eieio--class-public-custom-label newc))
533 (cl-callf nreverse (eieio--class-public-custom-group newc))
534 (cl-callf nreverse (eieio--class-public-printer newc))
535 (cl-callf nreverse (eieio--class-protection newc))
536 (cl-callf nreverse (eieio--class-initarg-tuples newc))
538 ;; The storage for class-class-allocation-type needs to be turned into
539 ;; a vector now.
540 (cl-callf (lambda (cat) (apply #'vector cat))
541 (eieio--class-class-allocation-type newc))
543 ;; Also, take class allocated values, and vectorize them for speed.
544 (cl-callf (lambda (cavs) (apply #'vector cavs))
545 (eieio--class-class-allocation-values newc))
547 ;; Attach slot symbols into a hashtable, and store the index of
548 ;; this slot as the value this table.
549 (let* ((cnt 0)
550 (pubsyms (eieio--class-public-a newc))
551 (prots (eieio--class-protection newc))
552 (oa (make-hash-table :test #'eq)))
553 (while pubsyms
554 (let ((newsym (list cnt)))
555 (setf (gethash (car pubsyms) oa) newsym)
556 (setq cnt (1+ cnt))
557 (if (car prots) (setcdr newsym (car prots))))
558 (setq pubsyms (cdr pubsyms)
559 prots (cdr prots)))
560 (setf (eieio--class-symbol-hashtable newc) oa))
562 ;; Set up a specialized doc string.
563 ;; Use stored value since it is calculated in a non-trivial way
564 (put cname 'variable-documentation
565 (eieio--class-option-assoc options :documentation))
567 ;; Save the file location where this class is defined.
568 (let ((fname (if load-in-progress
569 load-file-name
570 buffer-file-name)))
571 (when fname
572 (when (string-match "\\.elc\\'" fname)
573 (setq fname (substring fname 0 (1- (length fname)))))
574 (put cname 'class-location fname)))
576 ;; We have a list of custom groups. Store them into the options.
577 (let ((g (eieio--class-option-assoc options :custom-groups)))
578 (mapc (lambda (cg) (cl-pushnew cg g :test 'equal)) groups)
579 (if (memq :custom-groups options)
580 (setcar (cdr (memq :custom-groups options)) g)
581 (setq options (cons :custom-groups (cons g options)))))
583 ;; Set up the options we have collected.
584 (setf (eieio--class-options newc) options)
586 ;; if this is a superclass, clear out parent (which was set to the
587 ;; default superclass eieio-default-superclass)
588 (if clearparent (setf (eieio--class-parent newc) nil))
590 ;; Create the cached default object.
591 (let ((cache (make-vector (+ (length (eieio--class-public-a newc))
592 (eval-when-compile eieio--object-num-slots))
593 nil))
594 ;; We don't strictly speaking need to use a symbol, but the old
595 ;; code used the class's name rather than the class's object, so
596 ;; we follow this preference for using a symbol, which is probably
597 ;; convenient to keep the printed representation of such Elisp
598 ;; objects readable.
599 (tag (intern (format "eieio-class-tag--%s" cname))))
600 (set tag newc)
601 (setf (eieio--object-class-tag cache) tag)
602 (let ((eieio-skip-typecheck t))
603 ;; All type-checking has been done to our satisfaction
604 ;; before this call. Don't waste our time in this call..
605 (eieio-set-defaults cache t))
606 (setf (eieio--class-default-object-cache newc) cache))
608 ;; Return our new class object
609 ;; newc
610 cname
613 (defsubst eieio-eval-default-p (val)
614 "Whether the default value VAL should be evaluated for use."
615 (and (consp val) (symbolp (car val)) (fboundp (car val))))
617 (defun eieio--perform-slot-validation-for-default (slot spec value skipnil)
618 "For SLOT, signal if SPEC does not match VALUE.
619 If SKIPNIL is non-nil, then if VALUE is nil return t instead."
620 (if (not (or (eieio-eval-default-p value) ;FIXME: Why?
621 eieio-skip-typecheck
622 (and skipnil (null value))
623 (eieio--perform-slot-validation spec value)))
624 (signal 'invalid-slot-type (list slot spec value))))
626 (defun eieio--add-new-slot (newc a d doc type cust label custg print prot init alloc
627 &optional defaultoverride skipnil)
628 "Add into NEWC attribute A.
629 If A already exists in NEWC, then do nothing. If it doesn't exist,
630 then also add in D (default), DOC, TYPE, CUST, LABEL, CUSTG, PRINT, PROT, and INIT arg.
631 Argument ALLOC specifies if the slot is allocated per instance, or per class.
632 If optional DEFAULTOVERRIDE is non-nil, then if A exists in NEWC,
633 we must override its value for a default.
634 Optional argument SKIPNIL indicates if type checking should be skipped
635 if default value is nil."
636 ;; Make sure we duplicate those items that are sequences.
637 (condition-case nil
638 (if (sequencep d) (setq d (copy-sequence d)))
639 ;; This copy can fail on a cons cell with a non-cons in the cdr. Let's skip it if it doesn't work.
640 (error nil))
641 (if (sequencep type) (setq type (copy-sequence type)))
642 (if (sequencep cust) (setq cust (copy-sequence cust)))
643 (if (sequencep custg) (setq custg (copy-sequence custg)))
645 ;; To prevent override information w/out specification of storage,
646 ;; we need to do this little hack.
647 (if (member a (eieio--class-class-allocation-a newc)) (setq alloc :class))
649 (if (or (not alloc) (and (symbolp alloc) (eq alloc :instance)))
650 ;; In this case, we modify the INSTANCE version of a given slot.
652 (progn
654 ;; Only add this element if it is so-far unique
655 (if (not (member a (eieio--class-public-a newc)))
656 (progn
657 (eieio--perform-slot-validation-for-default a type d skipnil)
658 (push a (eieio--class-public-a newc))
659 (push d (eieio--class-public-d newc))
660 (push doc (eieio--class-public-doc newc))
661 (push type (eieio--class-public-type newc))
662 (push cust (eieio--class-public-custom newc))
663 (push label (eieio--class-public-custom-label newc))
664 (push custg (eieio--class-public-custom-group newc))
665 (push print (eieio--class-public-printer newc))
666 (push prot (eieio--class-protection newc))
667 (setf (eieio--class-initarg-tuples newc) (cons (cons init a) (eieio--class-initarg-tuples newc)))
669 ;; When defaultoverride is true, we are usually adding new local
670 ;; attributes which must override the default value of any slot
671 ;; passed in by one of the parent classes.
672 (when defaultoverride
673 ;; There is a match, and we must override the old value.
674 (let* ((ca (eieio--class-public-a newc))
675 (np (member a ca))
676 (num (- (length ca) (length np)))
677 (dp (if np (nthcdr num (eieio--class-public-d newc))
678 nil))
679 (tp (if np (nth num (eieio--class-public-type newc))))
681 (if (not np)
682 (error "EIEIO internal error overriding default value for %s"
684 ;; If type is passed in, is it the same?
685 (if (not (eq type t))
686 (if (not (equal type tp))
687 (error
688 "Child slot type `%s' does not match inherited type `%s' for `%s'"
689 type tp a)))
690 ;; If we have a repeat, only update the initarg...
691 (unless (eq d eieio-unbound)
692 (eieio--perform-slot-validation-for-default a tp d skipnil)
693 (setcar dp d))
694 ;; If we have a new initarg, check for it.
695 (when init
696 (let* ((inits (eieio--class-initarg-tuples newc))
697 (inita (rassq a inits)))
698 ;; Replace the CAR of the associate INITA.
699 ;;(message "Initarg: %S replace %s" inita init)
700 (setcar inita init)
703 ;; PLN Tue Jun 26 11:57:06 2007 : The protection is
704 ;; checked and SHOULD match the superclass
705 ;; protection. Otherwise an error is thrown. However
706 ;; I wonder if a more flexible schedule might be
707 ;; implemented.
709 ;; EML - We used to have (if prot... here,
710 ;; but a prot of 'nil means public.
712 (let ((super-prot (nth num (eieio--class-protection newc)))
714 (if (not (eq prot super-prot))
715 (error "Child slot protection `%s' does not match inherited protection `%s' for `%s'"
716 prot super-prot a)))
717 ;; End original PLN
719 ;; PLN Tue Jun 26 11:57:06 2007 :
720 ;; Do a non redundant combination of ancient custom
721 ;; groups and new ones.
722 (when custg
723 (let* ((groups
724 (nthcdr num (eieio--class-public-custom-group newc)))
725 (list1 (car groups))
726 (list2 (if (listp custg) custg (list custg))))
727 (if (< (length list1) (length list2))
728 (setq list1 (prog1 list2 (setq list2 list1))))
729 (dolist (elt list2)
730 (unless (memq elt list1)
731 (push elt list1)))
732 (setcar groups list1)))
733 ;; End PLN
735 ;; PLN Mon Jun 25 22:44:34 2007 : If a new cust is
736 ;; set, simply replaces the old one.
737 (when cust
738 ;; (message "Custom type redefined to %s" cust)
739 (setcar (nthcdr num (eieio--class-public-custom newc)) cust))
741 ;; If a new label is specified, it simply replaces
742 ;; the old one.
743 (when label
744 ;; (message "Custom label redefined to %s" label)
745 (setcar (nthcdr num (eieio--class-public-custom-label newc)) label))
746 ;; End PLN
748 ;; PLN Sat Jun 30 17:24:42 2007 : when a new
749 ;; doc is specified, simply replaces the old one.
750 (when doc
751 ;;(message "Documentation redefined to %s" doc)
752 (setcar (nthcdr num (eieio--class-public-doc newc))
753 doc))
754 ;; End PLN
756 ;; If a new printer is specified, it simply replaces
757 ;; the old one.
758 (when print
759 ;; (message "printer redefined to %s" print)
760 (setcar (nthcdr num (eieio--class-public-printer newc)) print))
765 ;; CLASS ALLOCATED SLOTS
766 (let ((value (eieio-default-eval-maybe d)))
767 (if (not (member a (eieio--class-class-allocation-a newc)))
768 (progn
769 (eieio--perform-slot-validation-for-default a type value skipnil)
770 ;; Here we have found a :class version of a slot. This
771 ;; requires a very different approach.
772 (push a (eieio--class-class-allocation-a newc))
773 (push doc (eieio--class-class-allocation-doc newc))
774 (push type (eieio--class-class-allocation-type newc))
775 (push cust (eieio--class-class-allocation-custom newc))
776 (push label (eieio--class-class-allocation-custom-label newc))
777 (push custg (eieio--class-class-allocation-custom-group newc))
778 (push prot (eieio--class-class-allocation-protection newc))
779 ;; Default value is stored in the 'values section, since new objects
780 ;; can't initialize from this element.
781 (push value (eieio--class-class-allocation-values newc)))
782 (when defaultoverride
783 ;; There is a match, and we must override the old value.
784 (let* ((ca (eieio--class-class-allocation-a newc))
785 (np (member a ca))
786 (num (- (length ca) (length np)))
787 (dp (if np
788 (nthcdr num
789 (eieio--class-class-allocation-values newc))
790 nil))
791 (tp (if np (nth num (eieio--class-class-allocation-type newc))
792 nil)))
793 (if (not np)
794 (error "EIEIO internal error overriding default value for %s"
796 ;; If type is passed in, is it the same?
797 (if (not (eq type t))
798 (if (not (equal type tp))
799 (error
800 "Child slot type `%s' does not match inherited type `%s' for `%s'"
801 type tp a)))
802 ;; EML - Note: the only reason to override a class bound slot
803 ;; is to change the default, so allow unbound in.
805 ;; If we have a repeat, only update the value...
806 (eieio--perform-slot-validation-for-default a tp value skipnil)
807 (setcar dp value))
809 ;; PLN Tue Jun 26 11:57:06 2007 : The protection is
810 ;; checked and SHOULD match the superclass
811 ;; protection. Otherwise an error is thrown. However
812 ;; I wonder if a more flexible schedule might be
813 ;; implemented.
814 (let ((super-prot
815 (car (nthcdr num (eieio--class-class-allocation-protection newc)))))
816 (if (not (eq prot super-prot))
817 (error "Child slot protection `%s' does not match inherited protection `%s' for `%s'"
818 prot super-prot a)))
819 ;; Do a non redundant combination of ancient custom groups
820 ;; and new ones.
821 (when custg
822 (let* ((groups
823 (nthcdr num (eieio--class-class-allocation-custom-group newc)))
824 (list1 (car groups))
825 (list2 (if (listp custg) custg (list custg))))
826 (if (< (length list1) (length list2))
827 (setq list1 (prog1 list2 (setq list2 list1))))
828 (dolist (elt list2)
829 (unless (memq elt list1)
830 (push elt list1)))
831 (setcar groups list1)))
833 ;; PLN Sat Jun 30 17:24:42 2007 : when a new
834 ;; doc is specified, simply replaces the old one.
835 (when doc
836 ;;(message "Documentation redefined to %s" doc)
837 (setcar (nthcdr num (eieio--class-class-allocation-doc newc))
838 doc))
839 ;; End PLN
841 ;; If a new printer is specified, it simply replaces
842 ;; the old one.
843 (when print
844 ;; (message "printer redefined to %s" print)
845 (setcar (nthcdr num (eieio--class-class-allocation-printer newc)) print))
851 (defun eieio-copy-parents-into-subclass (newc _parents)
852 "Copy into NEWC the slots of PARENTS.
853 Follow the rules of not overwriting early parents when applying to
854 the new child class."
855 (let ((sn (eieio--class-option-assoc (eieio--class-options newc)
856 :allow-nil-initform)))
857 (dolist (pcv (eieio--class-parent newc))
858 ;; First, duplicate all the slots of the parent.
859 (let ((pa (eieio--class-public-a pcv))
860 (pd (eieio--class-public-d pcv))
861 (pdoc (eieio--class-public-doc pcv))
862 (ptype (eieio--class-public-type pcv))
863 (pcust (eieio--class-public-custom pcv))
864 (plabel (eieio--class-public-custom-label pcv))
865 (pcustg (eieio--class-public-custom-group pcv))
866 (printer (eieio--class-public-printer pcv))
867 (pprot (eieio--class-protection pcv))
868 (pinit (eieio--class-initarg-tuples pcv))
869 (i 0))
870 (while pa
871 (eieio--add-new-slot newc
872 (car pa) (car pd) (car pdoc) (aref ptype i)
873 (car pcust) (car plabel) (car pcustg)
874 (car printer)
875 (car pprot) (car-safe (car pinit)) nil nil sn)
876 ;; Increment each value.
877 (setq pa (cdr pa)
878 pd (cdr pd)
879 pdoc (cdr pdoc)
880 i (1+ i)
881 pcust (cdr pcust)
882 plabel (cdr plabel)
883 pcustg (cdr pcustg)
884 printer (cdr printer)
885 pprot (cdr pprot)
886 pinit (cdr pinit))
887 )) ;; while/let
888 ;; Now duplicate all the class alloc slots.
889 (let ((pa (eieio--class-class-allocation-a pcv))
890 (pdoc (eieio--class-class-allocation-doc pcv))
891 (ptype (eieio--class-class-allocation-type pcv))
892 (pcust (eieio--class-class-allocation-custom pcv))
893 (plabel (eieio--class-class-allocation-custom-label pcv))
894 (pcustg (eieio--class-class-allocation-custom-group pcv))
895 (printer (eieio--class-class-allocation-printer pcv))
896 (pprot (eieio--class-class-allocation-protection pcv))
897 (pval (eieio--class-class-allocation-values pcv))
898 (i 0))
899 (while pa
900 (eieio--add-new-slot newc
901 (car pa) (aref pval i) (car pdoc) (aref ptype i)
902 (car pcust) (car plabel) (car pcustg)
903 (car printer)
904 (car pprot) nil :class sn)
905 ;; Increment each value.
906 (setq pa (cdr pa)
907 pdoc (cdr pdoc)
908 pcust (cdr pcust)
909 plabel (cdr plabel)
910 pcustg (cdr pcustg)
911 printer (cdr printer)
912 pprot (cdr pprot)
913 i (1+ i))
914 )))))
917 ;;; Slot type validation
919 ;; This is a hideous hack for replacing `typep' from cl-macs, to avoid
920 ;; requiring the CL library at run-time. It can be eliminated if/when
921 ;; `typep' is merged into Emacs core.
923 (defun eieio--perform-slot-validation (spec value)
924 "Return non-nil if SPEC does not match VALUE."
925 (or (eq spec t) ; t always passes
926 (eq value eieio-unbound) ; unbound always passes
927 (cl-typep value spec)))
929 (defun eieio--validate-slot-value (class slot-idx value slot)
930 "Make sure that for CLASS referencing SLOT-IDX, VALUE is valid.
931 Checks the :type specifier.
932 SLOT is the slot that is being checked, and is only used when throwing
933 an error."
934 (if eieio-skip-typecheck
936 ;; Trim off object IDX junk added in for the object index.
937 (setq slot-idx (- slot-idx (eval-when-compile eieio--object-num-slots)))
938 (let ((st (aref (eieio--class-public-type class) slot-idx)))
939 (if (not (eieio--perform-slot-validation st value))
940 (signal 'invalid-slot-type
941 (list (eieio--class-symbol class) slot st value))))))
943 (defun eieio--validate-class-slot-value (class slot-idx value slot)
944 "Make sure that for CLASS referencing SLOT-IDX, VALUE is valid.
945 Checks the :type specifier.
946 SLOT is the slot that is being checked, and is only used when throwing
947 an error."
948 (if eieio-skip-typecheck
950 (let ((st (aref (eieio--class-class-allocation-type class)
951 slot-idx)))
952 (if (not (eieio--perform-slot-validation st value))
953 (signal 'invalid-slot-type
954 (list (eieio--class-symbol class) slot st value))))))
956 (defun eieio-barf-if-slot-unbound (value instance slotname fn)
957 "Throw a signal if VALUE is a representation of an UNBOUND slot.
958 INSTANCE is the object being referenced. SLOTNAME is the offending
959 slot. If the slot is ok, return VALUE.
960 Argument FN is the function calling this verifier."
961 (if (and (eq value eieio-unbound) (not eieio-skip-typecheck))
962 (slot-unbound instance (eieio--object-class-name instance) slotname fn)
963 value))
966 ;;; Get/Set slots in an object.
968 (defun eieio-oref (obj slot)
969 "Return the value in OBJ at SLOT in the object vector."
970 (eieio--check-type (or eieio-object-p class-p) obj)
971 (eieio--check-type symbolp slot)
972 (if (class-p obj) (eieio-class-un-autoload obj))
973 (let* ((class (cond ((symbolp obj)
974 (error "eieio-oref called on a class!")
975 (eieio--class-v obj))
976 (t (eieio--object-class-object obj))))
977 (c (eieio--slot-name-index class obj slot)))
978 (if (not c)
979 ;; It might be missing because it is a :class allocated slot.
980 ;; Let's check that info out.
981 (if (setq c (eieio--class-slot-name-index class slot))
982 ;; Oref that slot.
983 (aref (eieio--class-class-allocation-values class) c)
984 ;; The slot-missing method is a cool way of allowing an object author
985 ;; to intercept missing slot definitions. Since it is also the LAST
986 ;; thing called in this fn, its return value would be retrieved.
987 (slot-missing obj slot 'oref)
988 ;;(signal 'invalid-slot-name (list (eieio-object-name obj) slot))
990 (eieio--check-type eieio-object-p obj)
991 (eieio-barf-if-slot-unbound (aref obj c) obj slot 'oref))))
994 (defun eieio-oref-default (obj slot)
995 "Do the work for the macro `oref-default' with similar parameters.
996 Fills in OBJ's SLOT with its default value."
997 (eieio--check-type (or eieio-object-p class-p) obj)
998 (eieio--check-type symbolp slot)
999 (let* ((cl (cond ((symbolp obj) (eieio--class-v obj))
1000 (t (eieio--object-class-object obj))))
1001 (c (eieio--slot-name-index cl obj slot)))
1002 (if (not c)
1003 ;; It might be missing because it is a :class allocated slot.
1004 ;; Let's check that info out.
1005 (if (setq c
1006 (eieio--class-slot-name-index cl slot))
1007 ;; Oref that slot.
1008 (aref (eieio--class-class-allocation-values cl)
1010 (slot-missing obj slot 'oref-default)
1011 ;;(signal 'invalid-slot-name (list (class-name cl) slot))
1013 (eieio-barf-if-slot-unbound
1014 (let ((val (nth (- c (eval-when-compile eieio--object-num-slots))
1015 (eieio--class-public-d cl))))
1016 (eieio-default-eval-maybe val))
1017 obj (eieio--class-symbol cl) 'oref-default))))
1019 (defun eieio-default-eval-maybe (val)
1020 "Check VAL, and return what `oref-default' would provide."
1021 ;; FIXME: What the hell is this supposed to do? Shouldn't it evaluate
1022 ;; variables as well? Why not just always call `eval'?
1023 (cond
1024 ;; Is it a function call? If so, evaluate it.
1025 ((eieio-eval-default-p val)
1026 (eval val))
1027 ;;;; check for quoted things, and unquote them
1028 ;;((and (consp val) (eq (car val) 'quote))
1029 ;; (car (cdr val)))
1030 ;; return it verbatim
1031 (t val)))
1033 (defun eieio-oset (obj slot value)
1034 "Do the work for the macro `oset'.
1035 Fills in OBJ's SLOT with VALUE."
1036 (eieio--check-type eieio-object-p obj)
1037 (eieio--check-type symbolp slot)
1038 (let* ((class (eieio--object-class-object obj))
1039 (c (eieio--slot-name-index class obj slot)))
1040 (if (not c)
1041 ;; It might be missing because it is a :class allocated slot.
1042 ;; Let's check that info out.
1043 (if (setq c
1044 (eieio--class-slot-name-index class slot))
1045 ;; Oset that slot.
1046 (progn
1047 (eieio--validate-class-slot-value class c value slot)
1048 (aset (eieio--class-class-allocation-values class)
1049 c value))
1050 ;; See oref for comment on `slot-missing'
1051 (slot-missing obj slot 'oset value)
1052 ;;(signal 'invalid-slot-name (list (eieio-object-name obj) slot))
1054 (eieio--validate-slot-value class c value slot)
1055 (aset obj c value))))
1057 (defun eieio-oset-default (class slot value)
1058 "Do the work for the macro `oset-default'.
1059 Fills in the default value in CLASS' in SLOT with VALUE."
1060 (setq class (eieio--class-object class))
1061 (eieio--check-type eieio--class-p class)
1062 (eieio--check-type symbolp slot)
1063 (eieio--with-scoped-class class
1064 (let* ((c (eieio--slot-name-index class nil slot)))
1065 (if (not c)
1066 ;; It might be missing because it is a :class allocated slot.
1067 ;; Let's check that info out.
1068 (if (setq c (eieio--class-slot-name-index class slot))
1069 (progn
1070 ;; Oref that slot.
1071 (eieio--validate-class-slot-value class c value slot)
1072 (aset (eieio--class-class-allocation-values class) c
1073 value))
1074 (signal 'invalid-slot-name (list (eieio--class-symbol class) slot)))
1075 (eieio--validate-slot-value class c value slot)
1076 ;; Set this into the storage for defaults.
1077 (setcar (nthcdr (- c (eval-when-compile eieio--object-num-slots))
1078 (eieio--class-public-d class))
1079 value)
1080 ;; Take the value, and put it into our cache object.
1081 (eieio-oset (eieio--class-default-object-cache class)
1082 slot value)
1083 ))))
1086 ;;; EIEIO internal search functions
1088 (defun eieio--slot-originating-class-p (start-class slot)
1089 "Return non-nil if START-CLASS is the first class to define SLOT.
1090 This is for testing if the class currently in scope is the class that defines SLOT
1091 so that we can protect private slots."
1092 (let ((par (eieio--class-parent start-class))
1093 (ret t))
1094 (or (not par)
1095 (progn
1096 (while (and par ret)
1097 (if (gethash slot (eieio--class-symbol-hashtable (car par)))
1098 (setq ret nil))
1099 (setq par (cdr par)))
1100 ret))))
1102 (defun eieio--slot-name-index (class obj slot)
1103 "In CLASS for OBJ find the index of the named SLOT.
1104 The slot is a symbol which is installed in CLASS by the `defclass'
1105 call. OBJ can be nil, but if it is an object, and the slot in question
1106 is protected, access will be allowed if OBJ is a child of the currently
1107 scoped class.
1108 If SLOT is the value created with :initarg instead,
1109 reverse-lookup that name, and recurse with the associated slot value."
1110 ;; Removed checks to outside this call
1111 (let* ((fsym (gethash slot (eieio--class-symbol-hashtable class)))
1112 (fsi (car fsym)))
1113 (if (integerp fsi)
1114 (cond
1115 ((not (cdr fsym))
1116 (+ (eval-when-compile eieio--object-num-slots) fsi))
1117 ((and (eq (cdr fsym) 'protected)
1118 (eieio--scoped-class)
1119 (or (child-of-class-p class (eieio--scoped-class))
1120 (and (eieio-object-p obj)
1121 ;; AFAICT, for all callers, if `obj' is not a class,
1122 ;; then its class is `class'.
1123 ;;(child-of-class-p class (eieio--object-class-object obj))
1124 (progn
1125 (cl-assert (eq class (eieio--object-class-object obj)))
1126 t))))
1127 (+ (eval-when-compile eieio--object-num-slots) fsi))
1128 ((and (eq (cdr fsym) 'private)
1129 (or (and (eieio--scoped-class)
1130 (eieio--slot-originating-class-p
1131 (eieio--scoped-class) slot))
1132 eieio-initializing-object))
1133 (+ (eval-when-compile eieio--object-num-slots) fsi))
1134 (t nil))
1135 (let ((fn (eieio--initarg-to-attribute class slot)))
1136 (if fn (eieio--slot-name-index class obj fn) nil)))))
1138 (defun eieio--class-slot-name-index (class slot)
1139 "In CLASS find the index of the named SLOT.
1140 The slot is a symbol which is installed in CLASS by the `defclass'
1141 call. If SLOT is the value created with :initarg instead,
1142 reverse-lookup that name, and recurse with the associated slot value."
1143 ;; This will happen less often, and with fewer slots. Do this the
1144 ;; storage cheap way.
1145 (let* ((a (eieio--class-class-allocation-a class))
1146 (l1 (length a))
1147 (af (memq slot a))
1148 (l2 (length af)))
1149 ;; Slot # is length of the total list, minus the remaining list of
1150 ;; the found slot.
1151 (if af (- l1 l2))))
1154 ;; Way to assign slots based on a list. Used for constructors, or
1155 ;; even resetting an object at run-time
1157 (defun eieio-set-defaults (obj &optional set-all)
1158 "Take object OBJ, and reset all slots to their defaults.
1159 If SET-ALL is non-nil, then when a default is nil, that value is
1160 reset. If SET-ALL is nil, the slots are only reset if the default is
1161 not nil."
1162 (eieio--with-scoped-class (eieio--object-class-object obj)
1163 (let ((eieio-initializing-object t)
1164 (pub (eieio--class-public-a (eieio--object-class-object obj))))
1165 (while pub
1166 (let ((df (eieio-oref-default obj (car pub))))
1167 (if (or df set-all)
1168 (eieio-oset obj (car pub) df)))
1169 (setq pub (cdr pub))))))
1171 (defun eieio--initarg-to-attribute (class initarg)
1172 "For CLASS, convert INITARG to the actual attribute name.
1173 If there is no translation, pass it in directly (so we can cheat if
1174 need be... May remove that later...)"
1175 (let ((tuple (assoc initarg (eieio--class-initarg-tuples class))))
1176 (if tuple
1177 (cdr tuple)
1178 nil)))
1181 ;; Method Invocation order: C3
1182 (defun eieio--c3-candidate (class remaining-inputs)
1183 "Return CLASS if it can go in the result now, otherwise nil."
1184 ;; Ensure CLASS is not in any position but the first in any of the
1185 ;; element lists of REMAINING-INPUTS.
1186 (and (not (let ((found nil))
1187 (while (and remaining-inputs (not found))
1188 (setq found (member class (cdr (car remaining-inputs)))
1189 remaining-inputs (cdr remaining-inputs)))
1190 found))
1191 class))
1193 (defun eieio--c3-merge-lists (reversed-partial-result remaining-inputs)
1194 "Merge REVERSED-PARTIAL-RESULT REMAINING-INPUTS in a consistent order, if possible.
1195 If a consistent order does not exist, signal an error."
1196 (if (let ((tail remaining-inputs)
1197 (found nil))
1198 (while (and tail (not found))
1199 (setq found (car tail) tail (cdr tail)))
1200 (not found))
1201 ;; If all remaining inputs are empty lists, we are done.
1202 (nreverse reversed-partial-result)
1203 ;; Otherwise, we try to find the next element of the result. This
1204 ;; is achieved by considering the first element of each
1205 ;; (non-empty) input list and accepting a candidate if it is
1206 ;; consistent with the rests of the input lists.
1207 (let* ((found nil)
1208 (tail remaining-inputs)
1209 (next (progn
1210 (while (and tail (not found))
1211 (setq found (and (car tail)
1212 (eieio--c3-candidate (caar tail)
1213 remaining-inputs))
1214 tail (cdr tail)))
1215 found)))
1216 (if next
1217 ;; The graph is consistent so far, add NEXT to result and
1218 ;; merge input lists, dropping NEXT from their heads where
1219 ;; applicable.
1220 (eieio--c3-merge-lists
1221 (cons next reversed-partial-result)
1222 (mapcar (lambda (l) (if (eq (cl-first l) next) (cl-rest l) l))
1223 remaining-inputs))
1224 ;; The graph is inconsistent, give up
1225 (signal 'inconsistent-class-hierarchy (list remaining-inputs))))))
1227 (defun eieio--class-precedence-c3 (class)
1228 "Return all parents of CLASS in c3 order."
1229 (let ((parents (eieio--class-parent (eieio--class-v class))))
1230 (eieio--c3-merge-lists
1231 (list class)
1232 (append
1234 (mapcar #'eieio--class-precedence-c3 parents)
1235 `((,eieio-default-superclass)))
1236 (list parents))))
1239 ;; Method Invocation Order: Depth First
1241 (defun eieio--class-precedence-dfs (class)
1242 "Return all parents of CLASS in depth-first order."
1243 (let* ((parents (eieio--class-parent class))
1244 (classes (copy-sequence
1245 (apply #'append
1246 (list class)
1248 (mapcar
1249 (lambda (parent)
1250 (cons parent
1251 (eieio--class-precedence-dfs parent)))
1252 parents)
1253 `((,eieio-default-superclass))))))
1254 (tail classes))
1255 ;; Remove duplicates.
1256 (while tail
1257 (setcdr tail (delq (car tail) (cdr tail)))
1258 (setq tail (cdr tail)))
1259 classes))
1262 ;; Method Invocation Order: Breadth First
1263 (defun eieio--class-precedence-bfs (class)
1264 "Return all parents of CLASS in breadth-first order."
1265 (let* ((result)
1266 (queue (or (eieio--class-parent class)
1267 `(,eieio-default-superclass))))
1268 (while queue
1269 (let ((head (pop queue)))
1270 (unless (member head result)
1271 (push head result)
1272 (unless (eq head eieio-default-superclass)
1273 (setq queue (append queue (or (eieio--class-parent head)
1274 `(,eieio-default-superclass))))))))
1275 (cons class (nreverse result)))
1279 ;; Method Invocation Order
1281 (defun eieio--class-precedence-list (class)
1282 "Return (transitively closed) list of parents of CLASS.
1283 The order, in which the parents are returned depends on the
1284 method invocation orders of the involved classes."
1285 (if (or (null class) (eq class eieio-default-superclass))
1287 (cl-case (eieio--class-method-invocation-order class)
1288 (:depth-first
1289 (eieio--class-precedence-dfs class))
1290 (:breadth-first
1291 (eieio--class-precedence-bfs class))
1292 (:c3
1293 (eieio--class-precedence-c3 class))))
1295 (define-obsolete-function-alias
1296 'class-precedence-list 'eieio--class-precedence-list "24.4")
1299 ;;; Here are some special types of errors
1301 (define-error 'invalid-slot-name "Invalid slot name")
1302 (define-error 'invalid-slot-type "Invalid slot type")
1303 (define-error 'unbound-slot "Unbound slot")
1304 (define-error 'inconsistent-class-hierarchy "Inconsistent class hierarchy")
1306 ;;; Hooking into cl-generic.
1308 (require 'cl-generic)
1310 (add-function :before-until cl-generic-tagcode-function
1311 #'eieio--generic-tagcode)
1312 (defun eieio--generic-tagcode (type name)
1313 ;; CLHS says:
1314 ;; A class must be defined before it can be used as a parameter
1315 ;; specializer in a defmethod form.
1316 ;; So we can ignore types that are not known to denote classes.
1317 (and (class-p type)
1318 ;; Prefer (aref ,name 0) over (eieio--class-tag ,name) so that
1319 ;; the tagcode is identical to the tagcode used for cl-struct.
1320 `(50 . (and (vectorp ,name) (aref ,name 0)))))
1322 (add-function :before-until cl-generic-tag-types-function
1323 #'eieio--generic-tag-types)
1324 (defun eieio--generic-tag-types (tag)
1325 (and (symbolp tag) (boundp tag) (eieio--class-p (symbol-value tag))
1326 (mapcar #'eieio--class-symbol
1327 (eieio--class-precedence-list (symbol-value tag)))))
1329 ;;; Backward compatibility functions
1330 ;; To support .elc files compiled for older versions of EIEIO.
1332 (defun eieio-defclass (cname superclasses slots options)
1333 (declare (obsolete eieio-defclass-internal "25.1"))
1334 (eval `(defclass ,cname ,superclasses ,slots ,@options)))
1337 (provide 'eieio-core)
1339 ;;; eieio-core.el ends here