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