Don't call debug on failed cl-assert
[emacs.git] / lisp / emacs-lisp / cl-preloaded.el
blob639ffa64b8cbdae6d99cebde12e1460d0db1c466
1 ;;; cl-preloaded.el --- Preloaded part of the CL library -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2015-2016 Free Software Foundation, Inc
5 ;; Author: Stefan Monnier <monnier@iro.umontreal.ca>
6 ;; Package: emacs
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;;; Commentary:
25 ;; The cl-defstruct macro is full of circularities, since it uses the
26 ;; cl-structure-class type (and its accessors) which is defined with itself,
27 ;; and it setups a default parent (cl-structure-object) which is also defined
28 ;; with cl-defstruct, and to make things more interesting, the class of
29 ;; cl-structure-object is of course an object of type cl-structure-class while
30 ;; cl-structure-class's parent is cl-structure-object.
31 ;; Furthermore, the code generated by cl-defstruct generally assumes that the
32 ;; parent will be loaded when the child is loaded. But at the same time, the
33 ;; expectation is that structs defined with cl-defstruct do not need cl-lib at
34 ;; run-time, which means that the `cl-structure-object' parent can't be in
35 ;; cl-lib but should be preloaded. So here's this preloaded circular setup.
37 ;;; Code:
39 (eval-when-compile (require 'cl-lib))
40 (eval-when-compile (require 'cl-macs)) ;For cl--struct-class.
42 ;; The `assert' macro from the cl package signals
43 ;; `cl-assertion-failed' at runtime so always define it.
44 (define-error 'cl-assertion-failed (purecopy "Assertion failed"))
46 (defun cl--assertion-failed (form &optional string sargs args)
47 (if string
48 (apply #'error string (append sargs args))
49 (signal 'cl-assertion-failed `(,form ,@sargs))))
51 ;; When we load this (compiled) file during pre-loading, the cl--struct-class
52 ;; code below will need to access the `cl-struct' info, since it's considered
53 ;; already as its parent (because `cl-struct' was defined while the file was
54 ;; compiled). So let's temporarily setup a fake.
55 (defvar cl-struct-cl-structure-object-tags nil)
56 (unless (cl--find-class 'cl-structure-object)
57 (setf (cl--find-class 'cl-structure-object) 'dummy))
59 (fset 'cl--make-slot-desc
60 ;; To break circularity, we pre-define the slot constructor by hand.
61 ;; It's redefined a bit further down as part of the cl-defstruct of
62 ;; cl--slot-descriptor.
63 ;; BEWARE: Obviously, it's important to keep the two in sync!
64 (lambda (name &optional initform type props)
65 (vector 'cl-struct-cl-slot-descriptor
66 name initform type props)))
68 (defun cl--struct-get-class (name)
69 (or (if (not (symbolp name)) name)
70 (cl--find-class name)
71 (if (not (get name 'cl-struct-type))
72 ;; FIXME: Add a conversion for `eieio--class' so we can
73 ;; create a cl-defstruct that inherits from an eieio class?
74 (error "%S is not a struct name" name)
75 ;; Backward compatibility with a defstruct compiled with a version
76 ;; cl-defstruct from Emacs<25. Convert to new format.
77 (let ((tag (intern (format "cl-struct-%s" name)))
78 (type-and-named (get name 'cl-struct-type))
79 (descs (get name 'cl-struct-slots)))
80 (cl-struct-define name nil (get name 'cl-struct-include)
81 (unless (and (eq (car type-and-named) 'vector)
82 (null (cadr type-and-named))
83 (assq 'cl-tag-slot descs))
84 (car type-and-named))
85 (cadr type-and-named)
86 descs
87 (intern (format "cl-struct-%s-tags" name))
88 tag
89 (get name 'cl-struct-print))
90 (cl--find-class name)))))
92 (defun cl--plist-remove (plist member)
93 (cond
94 ((null plist) nil)
95 ((null member) plist)
96 ((eq plist member) (cddr plist))
97 (t `(,(car plist) ,(cadr plist) ,@(cl--plist-remove (cddr plist) member)))))
99 (defun cl--struct-register-child (parent tag)
100 ;; Can't use (cl-typep parent 'cl-structure-class) at this stage
101 ;; because `cl-structure-class' is defined later.
102 (while (vectorp parent)
103 (add-to-list (cl--struct-class-children-sym parent) tag)
104 ;; Only register ourselves as a child of the leftmost parent since structs
105 ;; can only only have one parent.
106 (setq parent (car (cl--struct-class-parents parent)))))
108 ;;;###autoload
109 (defun cl-struct-define (name docstring parent type named slots children-sym
110 tag print)
111 (cl-assert (or type (not named)))
112 (if (boundp children-sym)
113 (add-to-list children-sym tag)
114 (set children-sym (list tag)))
115 (and (null type) (eq (caar slots) 'cl-tag-slot)
116 ;; Hide the tag slot from "standard" (i.e. non-`type'd) structs.
117 (setq slots (cdr slots)))
118 (let* ((parent-class (when parent (cl--struct-get-class parent)))
119 (n (length slots))
120 (index-table (make-hash-table :test 'eq :size n))
121 (vslots (let ((v (make-vector n nil))
122 (i 0)
123 (offset (if type 0 1)))
124 (dolist (slot slots)
125 (let* ((props (cddr slot))
126 (typep (plist-member props :type))
127 (type (if typep (cadr typep) t)))
128 (aset v i (cl--make-slot-desc
129 (car slot) (nth 1 slot)
130 type (cl--plist-remove props typep))))
131 (puthash (car slot) (+ i offset) index-table)
132 (cl-incf i))
134 (class (cl--struct-new-class
135 name docstring
136 (unless (symbolp parent-class) (list parent-class))
137 type named vslots index-table children-sym tag print)))
138 (unless (symbolp parent-class)
139 (let ((pslots (cl--struct-class-slots parent-class)))
140 (or (>= n (length pslots))
141 (let ((ok t))
142 (dotimes (i (length pslots))
143 (unless (eq (cl--slot-descriptor-name (aref pslots i))
144 (cl--slot-descriptor-name (aref vslots i)))
145 (setq ok nil)))
147 (error "Included struct %S has changed since compilation of %S"
148 parent name))))
149 (add-to-list 'current-load-list `(define-type . ,name))
150 (cl--struct-register-child parent-class tag)
151 (unless (eq named t)
152 (eval `(defconst ,tag ',class) t)
153 ;; In the cl-generic support, we need to be able to check
154 ;; if a vector is a cl-struct object, without knowing its particular type.
155 ;; So we use the (otherwise) unused function slots of the tag symbol
156 ;; to put a special witness value, to make the check easy and reliable.
157 (fset tag :quick-object-witness-check))
158 (setf (cl--find-class name) class)))
160 (cl-defstruct (cl-structure-class
161 (:conc-name cl--struct-class-)
162 (:predicate cl--struct-class-p)
163 (:constructor nil)
164 (:constructor cl--struct-new-class
165 (name docstring parents type named slots index-table
166 children-sym tag print))
167 (:copier nil))
168 "The type of CL structs descriptors."
169 ;; The first few fields here are actually inherited from cl--class, but we
170 ;; have to define this one before, to break the circularity, so we manually
171 ;; list the fields here and later "backpatch" cl--class as the parent.
172 ;; BEWARE: Obviously, it's indispensable to keep these two structs in sync!
173 (name nil :type symbol) ;The type name.
174 (docstring nil :type string)
175 (parents nil :type (list-of cl--class)) ;The included struct.
176 (slots nil :type (vector cl--slot-descriptor))
177 (index-table nil :type hash-table)
178 (tag nil :type symbol) ;Placed in cl-tag-slot. Holds the struct-class object.
179 (type nil :type (memq (vector list)))
180 (named nil :type bool)
181 (print nil :type bool)
182 (children-sym nil :type symbol) ;This sym's value holds the tags of children.
185 (cl-defstruct (cl-structure-object
186 (:predicate cl-struct-p)
187 (:constructor nil)
188 (:copier nil))
189 "The root parent of all \"normal\" CL structs")
191 (setq cl--struct-default-parent 'cl-structure-object)
193 (cl-defstruct (cl-slot-descriptor
194 (:conc-name cl--slot-descriptor-)
195 (:constructor nil)
196 (:constructor cl--make-slot-descriptor
197 (name &optional initform type props))
198 (:copier cl--copy-slot-descriptor-1))
199 ;; FIXME: This is actually not used yet, for circularity reasons!
200 "Descriptor of structure slot."
201 name ;Attribute name (symbol).
202 initform
203 type
204 ;; Extra properties, kept in an alist, can include:
205 ;; :documentation, :protection, :custom, :label, :group, :printer.
206 (props nil :type alist))
208 (defun cl--copy-slot-descriptor (slot)
209 (let ((new (cl--copy-slot-descriptor-1 slot)))
210 (cl-callf copy-alist (cl--slot-descriptor-props new))
211 new))
213 (cl-defstruct (cl--class
214 (:constructor nil)
215 (:copier nil))
216 "Type of descriptors for any kind of structure-like data."
217 ;; Intended to be shared between defstruct and defclass.
218 (name nil :type symbol) ;The type name.
219 (docstring nil :type string)
220 ;; For structs there can only be one parent, but when EIEIO classes inherit
221 ;; from cl--class, we'll need this to hold a list.
222 (parents nil :type (list-of cl--class))
223 (slots nil :type (vector cl-slot-descriptor))
224 (index-table nil :type hash-table))
226 (cl-assert
227 (let ((sc-slots (cl--struct-class-slots (cl--find-class 'cl-structure-class)))
228 (c-slots (cl--struct-class-slots (cl--find-class 'cl--class)))
229 (eq t))
230 (dotimes (i (length c-slots))
231 (let ((sc-slot (aref sc-slots i))
232 (c-slot (aref c-slots i)))
233 (unless (eq (cl--slot-descriptor-name sc-slot)
234 (cl--slot-descriptor-name c-slot))
235 (setq eq nil))))
236 eq))
238 ;; Close the recursion between cl-structure-object and cl-structure-class.
239 (setf (cl--struct-class-parents (cl--find-class 'cl-structure-class))
240 (list (cl--find-class 'cl--class)))
241 (cl--struct-register-child
242 (cl--find-class 'cl--class)
243 (cl--struct-class-tag (cl--find-class 'cl-structure-class)))
245 (cl-assert (cl--find-class 'cl-structure-class))
246 (cl-assert (cl--find-class 'cl-structure-object))
247 (cl-assert (cl-struct-p (cl--find-class 'cl-structure-class)))
248 (cl-assert (cl-struct-p (cl--find-class 'cl-structure-object)))
249 (cl-assert (cl--class-p (cl--find-class 'cl-structure-class)))
250 (cl-assert (cl--class-p (cl--find-class 'cl-structure-object)))
252 ;; Make sure functions defined with cl-defsubst can be inlined even in
253 ;; packages which do not require CL. We don't put an autoload cookie
254 ;; directly on that function, since those cookies only go to cl-loaddefs.
255 (autoload 'cl--defsubst-expand "cl-macs")
256 ;; Autoload, so autoload.el and font-lock can use it even when CL
257 ;; is not loaded.
258 (put 'cl-defun 'doc-string-elt 3)
259 (put 'cl-defmacro 'doc-string-elt 3)
260 (put 'cl-defsubst 'doc-string-elt 3)
261 (put 'cl-defstruct 'doc-string-elt 2)
263 (provide 'cl-preloaded)
264 ;;; cl-preloaded.el ends here