copy-file now truncates output after writing
[emacs.git] / lisp / emacs-lisp / cl-preloaded.el
blobed0639b63ab0fc4c96fd91b3dc11886d2114bf8b
1 ;;; cl-preloaded.el --- Preloaded part of the CL library -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2015 Free Software Foundation, Inc
5 ;; Author: Stefan Monnier <monnier@iro.umontreal.ca>
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software: you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
22 ;;; Commentary:
24 ;; The cl-defstruct macro is full of circularities, since it uses the
25 ;; cl-structure-class type (and its accessors) which is defined with itself,
26 ;; and it setups a default parent (cl-structure-object) which is also defined
27 ;; with cl-defstruct, and to make things more interesting, the class of
28 ;; cl-structure-object is of course an object of type cl-structure-class while
29 ;; cl-structure-class's parent is cl-structure-object.
30 ;; Furthermore, the code generated by cl-defstruct generally assumes that the
31 ;; parent will be loaded when the child is loaded. But at the same time, the
32 ;; expectation is that structs defined with cl-defstruct do not need cl-lib at
33 ;; run-time, which means that the `cl-structure-object' parent can't be in
34 ;; cl-lib but should be preloaded. So here's this preloaded circular setup.
36 ;;; Code:
38 (eval-when-compile (require 'cl-lib))
39 (eval-when-compile (require 'cl-macs)) ;For cl--struct-class.
41 ;; The `assert' macro from the cl package signals
42 ;; `cl-assertion-failed' at runtime so always define it.
43 (define-error 'cl-assertion-failed (purecopy "Assertion failed"))
45 (defun cl--assertion-failed (form &optional string sargs args)
46 (if debug-on-error
47 (debug `(cl-assertion-failed ,form ,string ,@sargs))
48 (if string
49 (apply #'error string (append sargs args))
50 (signal 'cl-assertion-failed `(,form ,@sargs)))))
52 ;; When we load this (compiled) file during pre-loading, the cl--struct-class
53 ;; code below will need to access the `cl-struct' info, since it's considered
54 ;; already as its parent (because `cl-struct' was defined while the file was
55 ;; compiled). So let's temporarily setup a fake.
56 (defvar cl-struct-cl-structure-object-tags nil)
57 (unless (cl--find-class 'cl-structure-object)
58 (setf (cl--find-class 'cl-structure-object) 'dummy))
60 (fset 'cl--make-slot-desc
61 ;; To break circularity, we pre-define the slot constructor by hand.
62 ;; It's redefined a bit further down as part of the cl-defstruct of
63 ;; cl--slot-descriptor.
64 ;; BEWARE: Obviously, it's important to keep the two in sync!
65 (lambda (name &optional initform type props)
66 (vector 'cl-struct-cl-slot-descriptor
67 name initform type props)))
69 (defun cl--struct-get-class (name)
70 (or (if (not (symbolp name)) name)
71 (cl--find-class name)
72 (if (not (get name 'cl-struct-type))
73 ;; FIXME: Add a conversion for `eieio--class' so we can
74 ;; create a cl-defstruct that inherits from an eieio class?
75 (error "%S is not a struct name" name)
76 ;; Backward compatibility with a defstruct compiled with a version
77 ;; cl-defstruct from Emacs<25. Convert to new format.
78 (let ((tag (intern (format "cl-struct-%s" name)))
79 (type-and-named (get name 'cl-struct-type))
80 (descs (get name 'cl-struct-slots)))
81 (cl-struct-define name nil (get name 'cl-struct-include)
82 (unless (and (eq (car type-and-named) 'vector)
83 (null (cadr type-and-named))
84 (assq 'cl-tag-slot descs))
85 (car type-and-named))
86 (cadr type-and-named)
87 descs
88 (intern (format "cl-struct-%s-tags" name))
89 tag
90 (get name 'cl-struct-print))
91 (cl--find-class name)))))
93 (defun cl--plist-remove (plist member)
94 (cond
95 ((null plist) nil)
96 ((null member) plist)
97 ((eq plist member) (cddr plist))
98 (t `(,(car plist) ,(cadr plist) ,@(cl--plist-remove (cddr plist) member)))))
100 (defun cl--struct-register-child (parent tag)
101 ;; Can't use (cl-typep parent 'cl-structure-class) at this stage
102 ;; because `cl-structure-class' is defined later.
103 (while (vectorp parent)
104 (add-to-list (cl--struct-class-children-sym parent) tag)
105 ;; Only register ourselves as a child of the leftmost parent since structs
106 ;; can only only have one parent.
107 (setq parent (car (cl--struct-class-parents parent)))))
109 ;;;###autoload
110 (defun cl-struct-define (name docstring parent type named slots children-sym
111 tag print)
112 (cl-assert (or type (not named)))
113 (if (boundp children-sym)
114 (add-to-list children-sym tag)
115 (set children-sym (list tag)))
116 (and (null type) (eq (caar slots) 'cl-tag-slot)
117 ;; Hide the tag slot from "standard" (i.e. non-`type'd) structs.
118 (setq slots (cdr slots)))
119 (let* ((parent-class (when parent (cl--struct-get-class parent)))
120 (n (length slots))
121 (index-table (make-hash-table :test 'eq :size n))
122 (vslots (let ((v (make-vector n nil))
123 (i 0)
124 (offset (if type 0 1)))
125 (dolist (slot slots)
126 (let* ((props (cddr slot))
127 (typep (plist-member props :type))
128 (type (if typep (cadr typep) t)))
129 (aset v i (cl--make-slot-desc
130 (car slot) (nth 1 slot)
131 type (cl--plist-remove props typep))))
132 (puthash (car slot) (+ i offset) index-table)
133 (cl-incf i))
135 (class (cl--struct-new-class
136 name docstring
137 (unless (symbolp parent-class) (list parent-class))
138 type named vslots index-table children-sym tag print)))
139 (unless (symbolp parent-class)
140 (let ((pslots (cl--struct-class-slots parent-class)))
141 (or (>= n (length pslots))
142 (let ((ok t))
143 (dotimes (i (length pslots))
144 (unless (eq (cl--slot-descriptor-name (aref pslots i))
145 (cl--slot-descriptor-name (aref vslots i)))
146 (setq ok nil)))
148 (error "Included struct %S has changed since compilation of %S"
149 parent 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))
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 (cl-defstruct (cl--class
209 (:constructor nil)
210 (:copier nil))
211 "Type of descriptors for any kind of structure-like data."
212 ;; Intended to be shared between defstruct and defclass.
213 (name nil :type symbol) ;The type name.
214 (docstring nil :type string)
215 ;; For structs there can only be one parent, but when EIEIO classes inherit
216 ;; from cl--class, we'll need this to hold a list.
217 (parents nil :type (list-of cl--class))
218 (slots nil :type (vector cl-slot-descriptor))
219 (index-table nil :type hash-table))
221 (cl-assert
222 (let ((sc-slots (cl--struct-class-slots (cl--find-class 'cl-structure-class)))
223 (c-slots (cl--struct-class-slots (cl--find-class 'cl--class)))
224 (eq t))
225 (dotimes (i (length c-slots))
226 (let ((sc-slot (aref sc-slots i))
227 (c-slot (aref c-slots i)))
228 (unless (eq (cl--slot-descriptor-name sc-slot)
229 (cl--slot-descriptor-name c-slot))
230 (setq eq nil))))
231 eq))
233 ;; Close the recursion between cl-structure-object and cl-structure-class.
234 (setf (cl--struct-class-parents (cl--find-class 'cl-structure-class))
235 (list (cl--find-class 'cl--class)))
236 (cl--struct-register-child
237 (cl--find-class 'cl--class)
238 (cl--struct-class-tag (cl--find-class 'cl-structure-class)))
240 (cl-assert (cl--find-class 'cl-structure-class))
241 (cl-assert (cl--find-class 'cl-structure-object))
242 (cl-assert (cl-struct-p (cl--find-class 'cl-structure-class)))
243 (cl-assert (cl-struct-p (cl--find-class 'cl-structure-object)))
244 (cl-assert (cl--class-p (cl--find-class 'cl-structure-class)))
245 (cl-assert (cl--class-p (cl--find-class 'cl-structure-object)))
247 ;; Make sure functions defined with cl-defsubst can be inlined even in
248 ;; packages which do not require CL. We don't put an autoload cookie
249 ;; directly on that function, since those cookies only go to cl-loaddefs.
250 (autoload 'cl--defsubst-expand "cl-macs")
251 ;; Autoload, so autoload.el and font-lock can use it even when CL
252 ;; is not loaded.
253 (put 'cl-defun 'doc-string-elt 3)
254 (put 'cl-defmacro 'doc-string-elt 3)
255 (put 'cl-defsubst 'doc-string-elt 3)
256 (put 'cl-defstruct 'doc-string-elt 2)
258 (provide 'cl-preloaded)
259 ;;; cl-preloaded.el ends here