Remove compat code in Tramp
[emacs.git] / lisp / emacs-lisp / cl-preloaded.el
blob2b022c490536485fc45e8c207b81b90d98de668c
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 debug-on-error
48 (funcall debugger `(cl-assertion-failed ,form ,string ,@sargs))
49 (if string
50 (apply #'error string (append sargs args))
51 (signal 'cl-assertion-failed `(,form ,@sargs)))))
53 ;; When we load this (compiled) file during pre-loading, the cl--struct-class
54 ;; code below will need to access the `cl-struct' info, since it's considered
55 ;; already as its parent (because `cl-struct' was defined while the file was
56 ;; compiled). So let's temporarily setup a fake.
57 (defvar cl-struct-cl-structure-object-tags nil)
58 (unless (cl--find-class 'cl-structure-object)
59 (setf (cl--find-class 'cl-structure-object) 'dummy))
61 (fset 'cl--make-slot-desc
62 ;; To break circularity, we pre-define the slot constructor by hand.
63 ;; It's redefined a bit further down as part of the cl-defstruct of
64 ;; cl--slot-descriptor.
65 ;; BEWARE: Obviously, it's important to keep the two in sync!
66 (lambda (name &optional initform type props)
67 (vector 'cl-struct-cl-slot-descriptor
68 name initform type props)))
70 (defun cl--struct-get-class (name)
71 (or (if (not (symbolp name)) name)
72 (cl--find-class name)
73 (if (not (get name 'cl-struct-type))
74 ;; FIXME: Add a conversion for `eieio--class' so we can
75 ;; create a cl-defstruct that inherits from an eieio class?
76 (error "%S is not a struct name" name)
77 ;; Backward compatibility with a defstruct compiled with a version
78 ;; cl-defstruct from Emacs<25. Convert to new format.
79 (let ((tag (intern (format "cl-struct-%s" name)))
80 (type-and-named (get name 'cl-struct-type))
81 (descs (get name 'cl-struct-slots)))
82 (cl-struct-define name nil (get name 'cl-struct-include)
83 (unless (and (eq (car type-and-named) 'vector)
84 (null (cadr type-and-named))
85 (assq 'cl-tag-slot descs))
86 (car type-and-named))
87 (cadr type-and-named)
88 descs
89 (intern (format "cl-struct-%s-tags" name))
90 tag
91 (get name 'cl-struct-print))
92 (cl--find-class name)))))
94 (defun cl--plist-remove (plist member)
95 (cond
96 ((null plist) nil)
97 ((null member) plist)
98 ((eq plist member) (cddr plist))
99 (t `(,(car plist) ,(cadr plist) ,@(cl--plist-remove (cddr plist) member)))))
101 (defun cl--struct-register-child (parent tag)
102 ;; Can't use (cl-typep parent 'cl-structure-class) at this stage
103 ;; because `cl-structure-class' is defined later.
104 (while (vectorp parent)
105 (add-to-list (cl--struct-class-children-sym parent) tag)
106 ;; Only register ourselves as a child of the leftmost parent since structs
107 ;; can only only have one parent.
108 (setq parent (car (cl--struct-class-parents parent)))))
110 ;;;###autoload
111 (defun cl-struct-define (name docstring parent type named slots children-sym
112 tag print)
113 (cl-assert (or type (not named)))
114 (if (boundp children-sym)
115 (add-to-list children-sym tag)
116 (set children-sym (list tag)))
117 (and (null type) (eq (caar slots) 'cl-tag-slot)
118 ;; Hide the tag slot from "standard" (i.e. non-`type'd) structs.
119 (setq slots (cdr slots)))
120 (let* ((parent-class (when parent (cl--struct-get-class parent)))
121 (n (length slots))
122 (index-table (make-hash-table :test 'eq :size n))
123 (vslots (let ((v (make-vector n nil))
124 (i 0)
125 (offset (if type 0 1)))
126 (dolist (slot slots)
127 (let* ((props (cddr slot))
128 (typep (plist-member props :type))
129 (type (if typep (cadr typep) t)))
130 (aset v i (cl--make-slot-desc
131 (car slot) (nth 1 slot)
132 type (cl--plist-remove props typep))))
133 (puthash (car slot) (+ i offset) index-table)
134 (cl-incf i))
136 (class (cl--struct-new-class
137 name docstring
138 (unless (symbolp parent-class) (list parent-class))
139 type named vslots index-table children-sym tag print)))
140 (unless (symbolp parent-class)
141 (let ((pslots (cl--struct-class-slots parent-class)))
142 (or (>= n (length pslots))
143 (let ((ok t))
144 (dotimes (i (length pslots))
145 (unless (eq (cl--slot-descriptor-name (aref pslots i))
146 (cl--slot-descriptor-name (aref vslots i)))
147 (setq ok nil)))
149 (error "Included struct %S has changed since compilation of %S"
150 parent name))))
151 (add-to-list 'current-load-list `(define-type . ,name))
152 (cl--struct-register-child parent-class tag)
153 (unless (eq named t)
154 (eval `(defconst ,tag ',class) t)
155 ;; In the cl-generic support, we need to be able to check
156 ;; if a vector is a cl-struct object, without knowing its particular type.
157 ;; So we use the (otherwise) unused function slots of the tag symbol
158 ;; to put a special witness value, to make the check easy and reliable.
159 (fset tag :quick-object-witness-check))
160 (setf (cl--find-class name) class)))
162 (cl-defstruct (cl-structure-class
163 (:conc-name cl--struct-class-)
164 (:predicate cl--struct-class-p)
165 (:constructor nil)
166 (:constructor cl--struct-new-class
167 (name docstring parents type named slots index-table
168 children-sym tag print))
169 (:copier nil))
170 "The type of CL structs descriptors."
171 ;; The first few fields here are actually inherited from cl--class, but we
172 ;; have to define this one before, to break the circularity, so we manually
173 ;; list the fields here and later "backpatch" cl--class as the parent.
174 ;; BEWARE: Obviously, it's indispensable to keep these two structs in sync!
175 (name nil :type symbol) ;The type name.
176 (docstring nil :type string)
177 (parents nil :type (list-of cl--class)) ;The included struct.
178 (slots nil :type (vector cl--slot-descriptor))
179 (index-table nil :type hash-table)
180 (tag nil :type symbol) ;Placed in cl-tag-slot. Holds the struct-class object.
181 (type nil :type (memq (vector list)))
182 (named nil :type bool)
183 (print nil :type bool)
184 (children-sym nil :type symbol) ;This sym's value holds the tags of children.
187 (cl-defstruct (cl-structure-object
188 (:predicate cl-struct-p)
189 (:constructor nil)
190 (:copier nil))
191 "The root parent of all \"normal\" CL structs")
193 (setq cl--struct-default-parent 'cl-structure-object)
195 (cl-defstruct (cl-slot-descriptor
196 (:conc-name cl--slot-descriptor-)
197 (:constructor nil)
198 (:constructor cl--make-slot-descriptor
199 (name &optional initform type props))
200 (:copier cl--copy-slot-descriptor-1))
201 ;; FIXME: This is actually not used yet, for circularity reasons!
202 "Descriptor of structure slot."
203 name ;Attribute name (symbol).
204 initform
205 type
206 ;; Extra properties, kept in an alist, can include:
207 ;; :documentation, :protection, :custom, :label, :group, :printer.
208 (props nil :type alist))
210 (defun cl--copy-slot-descriptor (slot)
211 (let ((new (cl--copy-slot-descriptor-1 slot)))
212 (cl-callf copy-alist (cl--slot-descriptor-props new))
213 new))
215 (cl-defstruct (cl--class
216 (:constructor nil)
217 (:copier nil))
218 "Type of descriptors for any kind of structure-like data."
219 ;; Intended to be shared between defstruct and defclass.
220 (name nil :type symbol) ;The type name.
221 (docstring nil :type string)
222 ;; For structs there can only be one parent, but when EIEIO classes inherit
223 ;; from cl--class, we'll need this to hold a list.
224 (parents nil :type (list-of cl--class))
225 (slots nil :type (vector cl-slot-descriptor))
226 (index-table nil :type hash-table))
228 (cl-assert
229 (let ((sc-slots (cl--struct-class-slots (cl--find-class 'cl-structure-class)))
230 (c-slots (cl--struct-class-slots (cl--find-class 'cl--class)))
231 (eq t))
232 (dotimes (i (length c-slots))
233 (let ((sc-slot (aref sc-slots i))
234 (c-slot (aref c-slots i)))
235 (unless (eq (cl--slot-descriptor-name sc-slot)
236 (cl--slot-descriptor-name c-slot))
237 (setq eq nil))))
238 eq))
240 ;; Close the recursion between cl-structure-object and cl-structure-class.
241 (setf (cl--struct-class-parents (cl--find-class 'cl-structure-class))
242 (list (cl--find-class 'cl--class)))
243 (cl--struct-register-child
244 (cl--find-class 'cl--class)
245 (cl--struct-class-tag (cl--find-class 'cl-structure-class)))
247 (cl-assert (cl--find-class 'cl-structure-class))
248 (cl-assert (cl--find-class 'cl-structure-object))
249 (cl-assert (cl-struct-p (cl--find-class 'cl-structure-class)))
250 (cl-assert (cl-struct-p (cl--find-class 'cl-structure-object)))
251 (cl-assert (cl--class-p (cl--find-class 'cl-structure-class)))
252 (cl-assert (cl--class-p (cl--find-class 'cl-structure-object)))
254 ;; Make sure functions defined with cl-defsubst can be inlined even in
255 ;; packages which do not require CL. We don't put an autoload cookie
256 ;; directly on that function, since those cookies only go to cl-loaddefs.
257 (autoload 'cl--defsubst-expand "cl-macs")
258 ;; Autoload, so autoload.el and font-lock can use it even when CL
259 ;; is not loaded.
260 (put 'cl-defun 'doc-string-elt 3)
261 (put 'cl-defmacro 'doc-string-elt 3)
262 (put 'cl-defsubst 'doc-string-elt 3)
263 (put 'cl-defstruct 'doc-string-elt 2)
265 (provide 'cl-preloaded)
266 ;;; cl-preloaded.el ends here