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