aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAndrea Corallo2024-02-15 16:08:00 +0100
committerAndrea Corallo2024-02-15 16:48:36 +0100
commit8a63e50036f0d4284f21660efb5dd20b63748d1b (patch)
treeeddf3789c13c1eb995477b1faaee632d23939652
parent98c906e5be2a3f5a14ff0172fdab38507b7746e3 (diff)
downloademacs-8a63e50036f0d4284f21660efb5dd20b63748d1b.tar.gz
emacs-8a63e50036f0d4284f21660efb5dd20b63748d1b.zip
* Define 'cl--type-hierarchy' and compute 'cl--typeof-types' from it
* lisp/emacs-lisp/cl-preloaded.el (cl--type-hierarchy) (cl--direct-supertypes-of-type, cl--direct-subtypes-of-type): Define. (cl--typeof-types): Compute automatically. (cl--supertypes-for-typeof-types): New function.
-rw-r--r--lisp/emacs-lisp/cl-preloaded.el97
1 files changed, 64 insertions, 33 deletions
diff --git a/lisp/emacs-lisp/cl-preloaded.el b/lisp/emacs-lisp/cl-preloaded.el
index 20e68555578..248c1fd7c24 100644
--- a/lisp/emacs-lisp/cl-preloaded.el
+++ b/lisp/emacs-lisp/cl-preloaded.el
@@ -50,44 +50,75 @@
50 (apply #'error string (append sargs args)) 50 (apply #'error string (append sargs args))
51 (signal 'cl-assertion-failed `(,form ,@sargs))))) 51 (signal 'cl-assertion-failed `(,form ,@sargs)))))
52 52
53(defconst cl--typeof-types 53
54 ;; Hand made from the source code of `type-of'. 54(defconst cl--type-hierarchy
55 '((integer number integer-or-marker number-or-marker atom) 55 ;; Please run `sycdoc-update-type-hierarchy' in
56 (symbol-with-pos symbol atom) (symbol atom) (string array sequence atom) 56 ;; etc/syncdoc-type-hierarchy.el each time this is updated to
57 (cons list sequence) 57 ;; reflect in the documentation.
58 ;; Markers aren't `numberp', yet they are accepted wherever integers are 58 '((t sequence atom)
59 ;; accepted, pretty much. 59 (sequence list array)
60 (marker integer-or-marker number-or-marker atom) 60 (atom
61 (overlay atom) (float number number-or-marker atom) 61 class structure tree-sitter-compiled-query tree-sitter-node
62 (window-configuration atom) (process atom) (window atom) 62 tree-sitter-parser user-ptr font-object font-entity font-spec
63 ;; FIXME: We'd want to put `function' here, but that's only true 63 condvar mutex thread terminal hash-table frame buffer function
64 ;; for those `subr's which aren't special forms! 64 window process window-configuration overlay integer-or-marker
65 (subr atom) 65 number-or-marker symbol array)
66 ;; FIXME: We should probably reverse the order between 66 (number float integer)
67 ;; `compiled-function' and `byte-code-function' since arguably 67 (number-or-marker marker number)
68 ;; `subr' is also "compiled functions" but not "byte code functions", 68 (integer bignum fixum)
69 ;; but it would require changing the value returned by `type-of' for 69 (symbol keyword boolean symbol-with-pos)
70 ;; byte code objects, which risks breaking existing code, which doesn't 70 (array vector bool-vector char-table string)
71 ;; seem worth the trouble. 71 (list null cons)
72 (compiled-function byte-code-function function atom) 72 (integer-or-marker integer marker)
73 (module-function function atom) 73 (compiled-function byte-code-function)
74 (buffer atom) (char-table array sequence atom) 74 (function subr module-function compiled-function)
75 (bool-vector array sequence atom) 75 (boolean null)
76 (frame atom) (hash-table atom) (terminal atom) 76 (subr subr-native-elisp subr-primitive)
77 (thread atom) (mutex atom) (condvar atom) 77 (symbol-with-pos keyword))
78 (font-spec atom) (font-entity atom) (font-object atom) 78 "List of lists describing all the edges of the builtin type
79 (vector array sequence atom) 79hierarchy.
80 (user-ptr atom) 80Each sublist is in the form (TYPE . DIRECT_SUBTYPES)"
81 (tree-sitter-parser atom) 81 ;; Given type hierarchy is a DAG (but mostly a tree) I believe this
82 (tree-sitter-node atom) 82 ;; is the most compact way to express it.
83 (tree-sitter-compiled-query atom) 83 )
84 ;; Plus, really hand made: 84
85 (null symbol list sequence atom)) 85(defconst cl--direct-supertypes-of-type
86 (make-hash-table :test #'eq)
87 "Hash table TYPE -> SUPERTYPES.")
88
89(defconst cl--direct-subtypes-of-type
90 (make-hash-table :test #'eq)
91 "Hash table TYPE -> SUBTYPES.")
92
93(cl-loop for (parent . children) in cl--type-hierarchy
94 do (cl-loop
95 for child in children
96 do (cl-pushnew parent (gethash child cl--direct-supertypes-of-type))
97 do (cl-pushnew child (gethash parent cl--direct-subtypes-of-type))))
98
99(defconst cl--typeof-types nil
86 "Alist of supertypes. 100 "Alist of supertypes.
87Each element has the form (TYPE . SUPERTYPES) where TYPE is one of 101Each element has the form (TYPE . SUPERTYPES) where TYPE is one of
88the symbols returned by `type-of', and SUPERTYPES is the list of its 102the symbols returned by `type-of', and SUPERTYPES is the list of its
89supertypes from the most specific to least specific.") 103supertypes from the most specific to least specific.")
90 104
105(defun cl--supertypes-for-typeof-types (type)
106 (cl-loop with res = ()
107 with agenda = (list type)
108 while agenda
109 for element = (car agenda)
110 unless (or (eq element t) ;; no t in `cl--typeof-types'.
111 (memq element res))
112 append (list element) into res
113 do (cl-loop for c in (gethash element cl--direct-supertypes-of-type)
114 do (setq agenda (append agenda (list c))))
115 do (setq agenda (cdr agenda))
116 finally (cl-return res)))
117
118(maphash (lambda (type _)
119 (push (cl--supertypes-for-typeof-types type) cl--typeof-types))
120 cl--direct-supertypes-of-type)
121
91(defconst cl--all-builtin-types 122(defconst cl--all-builtin-types
92 (delete-dups (copy-sequence (apply #'append cl--typeof-types)))) 123 (delete-dups (copy-sequence (apply #'append cl--typeof-types))))
93 124