aboutsummaryrefslogtreecommitdiffstats
path: root/src/data.c
diff options
context:
space:
mode:
authorStefan Monnier2024-03-12 09:26:24 -0400
committerStefan Monnier2024-03-18 09:29:47 -0400
commit706403f2aa3a306369a0150022da0cba1802ca2b (patch)
treec2a2a7e1dc919efdead6ee36f1fb91e3f624c673 /src/data.c
parent1a8b34a503e5af32851c1aac27a3f09e2345673b (diff)
downloademacs-706403f2aa3a306369a0150022da0cba1802ca2b.tar.gz
emacs-706403f2aa3a306369a0150022da0cba1802ca2b.zip
(cl-type-of): New function to return more precise types (bug#69739)
* src/data.c (Fcl_type_of): New function, extracted from `Ftype_of`. Make it return more precise types for symbols, integers, and subrs. (Ftype_of): Use it. (syms_of_data): Define the corresponding new symbols and defsubr the new function. * doc/lispref/objects.texi (Type Predicates): Document it. * src/comp.c (emit_limple_insn): Use `Fcl_type_of`. * lisp/emacs-lisp/cl-preloaded.el (subr): Demote it to `atom`. (subr-native-elisp, subr-primitive): Add `compiled-function` as parent instead. (special-form): New type. * lisp/obsolete/eieio-core.el (cl--generic-struct-tag): * lisp/emacs-lisp/cl-generic.el (cl--generic-typeof-generalizer): Use `cl-type-of`. cl--generic--unreachable-types): Update accordingly. test/src/data-tests.el (data-tests--cl-type-of): New test.
Diffstat (limited to 'src/data.c')
-rw-r--r--src/data.c40
1 files changed, 35 insertions, 5 deletions
diff --git a/src/data.c b/src/data.c
index 35f4c82c68f..5d6b6e0ba9d 100644
--- a/src/data.c
+++ b/src/data.c
@@ -193,16 +193,37 @@ DEFUN ("null", Fnull, Snull, 1, 1, 0,
193DEFUN ("type-of", Ftype_of, Stype_of, 1, 1, 0, 193DEFUN ("type-of", Ftype_of, Stype_of, 1, 1, 0,
194 doc: /* Return a symbol representing the type of OBJECT. 194 doc: /* Return a symbol representing the type of OBJECT.
195The symbol returned names the object's basic type; 195The symbol returned names the object's basic type;
196for example, (type-of 1) returns `integer'. */) 196for example, (type-of 1) returns `integer'.
197Contrary to `cl-type-of', the returned type is not always the most
198precise type possible, because instead this function tries to preserve
199compatibility with the return value of previous Emacs versions. */)
200 (Lisp_Object object)
201{
202 return SYMBOLP (object) ? Qsymbol
203 : INTEGERP (object) ? Qinteger
204 : SUBRP (object) ? Qsubr
205 : Fcl_type_of (object);
206}
207
208DEFUN ("cl-type-of", Fcl_type_of, Scl_type_of, 1, 1, 0,
209 doc: /* Return a symbol representing the type of OBJECT.
210The returned symbol names the most specific possible type of the object.
211for example, (cl-type-of nil) returns `null'.
212The specific type returned may change depending on Emacs versions,
213so we recommend you use `cl-typep', `cl-typecase', or other predicates
214rather than compare the return value of this function against
215a fixed set of types. */)
197 (Lisp_Object object) 216 (Lisp_Object object)
198{ 217{
199 switch (XTYPE (object)) 218 switch (XTYPE (object))
200 { 219 {
201 case_Lisp_Int: 220 case_Lisp_Int:
202 return Qinteger; 221 return Qfixnum;
203 222
204 case Lisp_Symbol: 223 case Lisp_Symbol:
205 return Qsymbol; 224 return NILP (object) ? Qnull
225 : EQ (object, Qt) ? Qboolean
226 : Qsymbol;
206 227
207 case Lisp_String: 228 case Lisp_String:
208 return Qstring; 229 return Qstring;
@@ -215,7 +236,7 @@ for example, (type-of 1) returns `integer'. */)
215 switch (PSEUDOVECTOR_TYPE (XVECTOR (object))) 236 switch (PSEUDOVECTOR_TYPE (XVECTOR (object)))
216 { 237 {
217 case PVEC_NORMAL_VECTOR: return Qvector; 238 case PVEC_NORMAL_VECTOR: return Qvector;
218 case PVEC_BIGNUM: return Qinteger; 239 case PVEC_BIGNUM: return Qbignum;
219 case PVEC_MARKER: return Qmarker; 240 case PVEC_MARKER: return Qmarker;
220 case PVEC_SYMBOL_WITH_POS: return Qsymbol_with_pos; 241 case PVEC_SYMBOL_WITH_POS: return Qsymbol_with_pos;
221 case PVEC_OVERLAY: return Qoverlay; 242 case PVEC_OVERLAY: return Qoverlay;
@@ -224,7 +245,10 @@ for example, (type-of 1) returns `integer'. */)
224 case PVEC_WINDOW_CONFIGURATION: return Qwindow_configuration; 245 case PVEC_WINDOW_CONFIGURATION: return Qwindow_configuration;
225 case PVEC_PROCESS: return Qprocess; 246 case PVEC_PROCESS: return Qprocess;
226 case PVEC_WINDOW: return Qwindow; 247 case PVEC_WINDOW: return Qwindow;
227 case PVEC_SUBR: return Qsubr; 248 case PVEC_SUBR:
249 return XSUBR (object)->max_args == UNEVALLED ? Qspecial_form
250 : SUBR_NATIVE_COMPILEDP (object) ? Qsubr_native_elisp
251 : Qsubr_primitive;
228 case PVEC_COMPILED: return Qcompiled_function; 252 case PVEC_COMPILED: return Qcompiled_function;
229 case PVEC_BUFFER: return Qbuffer; 253 case PVEC_BUFFER: return Qbuffer;
230 case PVEC_CHAR_TABLE: return Qchar_table; 254 case PVEC_CHAR_TABLE: return Qchar_table;
@@ -4202,7 +4226,9 @@ syms_of_data (void)
4202 "Variable binding depth exceeds max-specpdl-size"); 4226 "Variable binding depth exceeds max-specpdl-size");
4203 4227
4204 /* Types that type-of returns. */ 4228 /* Types that type-of returns. */
4229 DEFSYM (Qboolean, "boolean");
4205 DEFSYM (Qinteger, "integer"); 4230 DEFSYM (Qinteger, "integer");
4231 DEFSYM (Qbignum, "bignum");
4206 DEFSYM (Qsymbol, "symbol"); 4232 DEFSYM (Qsymbol, "symbol");
4207 DEFSYM (Qstring, "string"); 4233 DEFSYM (Qstring, "string");
4208 DEFSYM (Qcons, "cons"); 4234 DEFSYM (Qcons, "cons");
@@ -4218,6 +4244,9 @@ syms_of_data (void)
4218 DEFSYM (Qprocess, "process"); 4244 DEFSYM (Qprocess, "process");
4219 DEFSYM (Qwindow, "window"); 4245 DEFSYM (Qwindow, "window");
4220 DEFSYM (Qsubr, "subr"); 4246 DEFSYM (Qsubr, "subr");
4247 DEFSYM (Qspecial_form, "special-form");
4248 DEFSYM (Qsubr_primitive, "subr-primitive");
4249 DEFSYM (Qsubr_native_elisp, "subr-native-elisp");
4221 DEFSYM (Qcompiled_function, "compiled-function"); 4250 DEFSYM (Qcompiled_function, "compiled-function");
4222 DEFSYM (Qbuffer, "buffer"); 4251 DEFSYM (Qbuffer, "buffer");
4223 DEFSYM (Qframe, "frame"); 4252 DEFSYM (Qframe, "frame");
@@ -4255,6 +4284,7 @@ syms_of_data (void)
4255 defsubr (&Seq); 4284 defsubr (&Seq);
4256 defsubr (&Snull); 4285 defsubr (&Snull);
4257 defsubr (&Stype_of); 4286 defsubr (&Stype_of);
4287 defsubr (&Scl_type_of);
4258 defsubr (&Slistp); 4288 defsubr (&Slistp);
4259 defsubr (&Snlistp); 4289 defsubr (&Snlistp);
4260 defsubr (&Sconsp); 4290 defsubr (&Sconsp);