Except as noted, the @code{cl-defstruct} facility of this package is
entirely compatible with that of Common Lisp.
+The @code{cl-defstruct} package also provides a few structure
+introspection functions.
+
+@defun cl-struct-sequence-type struct-type
+This function returns the underlying data structure for
+@code{struct-type}, which is a symbol. It returns @code{vector} or
+@code{list}, or @code{nil} if @code{struct-type} is not actually a
+structure.
+
+@defun cl-struct-slot-info struct-type
+This function returns a list of slot descriptors for structure
+@code{struct-type}. Each entry in the list is @code{(name . opts)},
+where @code{name} is the name of the slot and @code{opts} is the list
+of slot options given to @code{defstruct}. Dummy entries represent
+the slots used for the struct name and that are skipped to implement
+@code{:initial-offset}.
+
+@defun cl-struct-slot-offset struct-type slot-name
+Return the offset of slot @code{slot-name} in @code{struct-type}. The
+returned zero-based slot index is relative to the start of the
+structure data type and is adjusted for any structure name and
+:initial-offset slots. Signal error if struct @code{struct-type} does
+not contain @code{slot-name}.
+
+@defun cl-struct-slot-value struct-type slot-name inst
+Return the value of slot @code{slot-name} in @code{inst} of
+@code{struct-type}. @code{struct} and @code{slot-name} are symbols.
+@code{inst} is a structure instance. This routine is also a
+@code{setf} place. @code{cl-struct-slot-value} uses
+@code{cl-struct-slot-offset} internally and can signal the same
+errors.
+
+@defun cl-struct-set-slot-value struct-type slot-name inst value
+Set the value of slot @code{slot-name} in @code{inst} of
+@code{struct-type}. @code{struct} and @code{slot-name} are symbols.
+@code{inst} is a structure instance. @code{value} is the value to
+which to set the given slot. Return @code{value}.
+@code{cl-struct-slot-value} uses @code{cl-struct-set-slot-offset}
+internally and can signal the same errors.
+
@node Assertions
@chapter Assertions and Errors
((symbolp x) (and (memq x '(nil t)) t))
(t t)))
-(defun cl--const-expr-val (x)
- (and (macroexp-const-p x) (if (consp x) (nth 1 x) x)))
+(defun cl--const-expr-val (x &optional environment default)
+ "Return the value of X known at compile-time.
+If X is not known at compile time, return DEFAULT. Before
+testing whether X is known at compile time, macroexpand it in
+ENVIRONMENT."
+ (let ((x (macroexpand-all x environment)))
+ (if (macroexp-const-p x)
+ (if (consp x) (nth 1 x) x)
+ default)))
(defun cl--expr-contains (x y)
"Count number of times X refers to Y. Return nil for 0 times."
look
`(or ,look
,(if (eq (cl--const-expr-p def) t)
- `'(nil ,(cl--const-expr-val def))
+ `'(nil ,(cl--const-expr-val
+ def macroexpand-all-environment))
`(list nil ,def))))))))
(push karg keys)))))
(setq keys (nreverse keys))
(declare (debug t))
(cons 'progn body))
;;;###autoload
-(defmacro cl-the (_type form)
- "At present this ignores TYPE and is simply equivalent to FORM."
+(defmacro cl-the (type form)
+ "Return FORM. If type-checking is enabled, assert that it is of TYPE."
(declare (indent 1) (debug (cl-type-spec form)))
- form)
+ (if (not (or (not (cl--compiling-file))
+ (< cl--optimize-speed 3)
+ (= cl--optimize-safety 3)))
+ form
+ (let* ((temp (if (cl--simple-expr-p form 3)
+ form (make-symbol "--cl-var--")))
+ (body `(progn (unless ,(cl--make-type-test temp type)
+ (signal 'wrong-type-argument
+ (list ',type ,temp ',form)))
+ ,temp)))
+ (if (eq temp form) body
+ `(let ((,temp ,form)) ,body)))))
(defvar cl--proclaim-history t) ; for future compilers
(defvar cl--declare-stack t) ; for future compilers
forms)
`(progn ,@(nreverse (cons `',name forms)))))
+(defun cl-struct-sequence-type (struct-type)
+ "Return the sequence used to build STRUCT-TYPE.
+STRUCT-TYPE is a symbol naming a struct type. Return 'vector or
+'list, or nil if STRUCT-TYPE is not a struct type. "
+ (car (get struct-type 'cl-struct-type)))
+(put 'cl-struct-sequence-type 'side-effect-free t)
+
+(defun cl-struct-slot-info (struct-type)
+ "Return a list of slot names of struct STRUCT-TYPE.
+Each entry is a list (SLOT-NAME . OPTS), where SLOT-NAME is a
+slot name symbol and OPTS is a list of slot options given to
+`cl-defstruct'. Dummy slots that represent the struct name and
+slots skipped by :initial-offset may appear in the list."
+ (get struct-type 'cl-struct-slots))
+(put 'cl-struct-slot-info 'side-effect-free t)
+
+(defun cl-struct-slot-offset (struct-type slot-name)
+ "Return the offset of slot SLOT-NAME in STRUCT-TYPE.
+The returned zero-based slot index is relative to the start of
+the structure data type and is adjusted for any structure name
+and :initial-offset slots. Signal error if struct STRUCT-TYPE
+does not contain SLOT-NAME."
+ (or (cl-position slot-name
+ (cl-struct-slot-info struct-type)
+ :key #'car :test #'eq)
+ (error "struct %s has no slot %s" struct-type slot-name)))
+(put 'cl-struct-slot-offset 'side-effect-free t)
+
+(defun cl-struct-slot-value (struct-type slot-name inst)
+ "Return the value of slot SLOT-NAME in INST of STRUCT-TYPE.
+STRUCT and SLOT-NAME are symbols. INST is a structure instance."
+ (unless (cl-typep inst struct-type)
+ (signal 'wrong-type-argument (list struct-type inst)))
+ (elt inst (cl-struct-slot-offset struct-type slot-name)))
+(put 'cl-struct-slot-value 'side-effect-free t)
+
+(defun cl-struct-set-slot-value (struct-type slot-name inst value)
+ "Set the value of slot SLOT-NAME in INST of STRUCT-TYPE.
+STRUCT and SLOT-NAME are symbols. INST is a structure instance.
+VALUE is the value to which to set the given slot. Return
+VALUE."
+ (unless (cl-typep inst struct-type)
+ (signal 'wrong-type-argument (list struct-type inst)))
+ (setf (elt inst (cl-struct-slot-offset struct-type slot-name)) value))
+
+(defsetf cl-struct-slot-value cl-struct-set-slot-value)
+
+(cl-define-compiler-macro cl-struct-slot-value
+ (&whole orig struct-type slot-name inst)
+ (or (let* ((macenv macroexpand-all-environment)
+ (struct-type (cl--const-expr-val struct-type macenv))
+ (slot-name (cl--const-expr-val slot-name macenv)))
+ (and struct-type (symbolp struct-type)
+ slot-name (symbolp slot-name)
+ (assq slot-name (cl-struct-slot-info struct-type))
+ (let ((idx (cl-struct-slot-offset struct-type slot-name)))
+ (cl-ecase (cl-struct-sequence-type struct-type)
+ (vector `(aref (cl-the ,struct-type ,inst) ,idx))
+ (list `(nth ,idx (cl-the ,struct-type ,inst)))))))
+ orig))
+
+(cl-define-compiler-macro cl-struct-set-slot-value
+ (&whole orig struct-type slot-name inst value)
+ (or (let* ((macenv macroexpand-all-environment)
+ (struct-type (cl--const-expr-val struct-type macenv))
+ (slot-name (cl--const-expr-val slot-name macenv)))
+ (and struct-type (symbolp struct-type)
+ slot-name (symbolp slot-name)
+ (assq slot-name (cl-struct-slot-info struct-type))
+ (let ((idx (cl-struct-slot-offset struct-type slot-name)))
+ (cl-ecase (cl-struct-sequence-type struct-type)
+ (vector `(setf (aref (cl-the ,struct-type ,inst) ,idx)
+ ,value))
+ (list `(setf (nth ,idx (cl-the ,struct-type ,inst))
+ ,value))))))
+ orig))
+
;;; Types and assertions.
;;;###autoload
(defun cl--compiler-macro-typep (form val type)
(if (macroexp-const-p type)
(macroexp-let2 macroexp-copyable-p temp val
- (cl--make-type-test temp (cl--const-expr-val type)))
+ (cl--make-type-test temp (cl--const-expr-val
+ type macroexpand-all-environment)))
form))
;;;###autoload
(defun cl--compiler-macro-member (form a list &rest keys)
(let ((test (and (= (length keys) 2) (eq (car keys) :test)
- (cl--const-expr-val (nth 1 keys)))))
+ (cl--const-expr-val (nth 1 keys)
+ macroexpand-all-environment))))
(cond ((eq test 'eq) `(memq ,a ,list))
((eq test 'equal) `(member ,a ,list))
((or (null keys) (eq test 'eql)) `(memql ,a ,list))
(defun cl--compiler-macro-assoc (form a list &rest keys)
(let ((test (and (= (length keys) 2) (eq (car keys) :test)
- (cl--const-expr-val (nth 1 keys)))))
+ (cl--const-expr-val (nth 1 keys)
+ macroexpand-all-environment))))
(cond ((eq test 'eq) `(assq ,a ,list))
((eq test 'equal) `(assoc ,a ,list))
((and (macroexp-const-p a) (or (null keys) (eq test 'eql)))
- (if (floatp (cl--const-expr-val a))
+ (if (floatp (cl--const-expr-val a macroexpand-all-environment))
`(assoc ,a ,list) `(assq ,a ,list)))
(t form))))