-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathphpinspect-typedef.el
522 lines (424 loc) · 22.1 KB
/
phpinspect-typedef.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
;;; phpinspect-typedef.el --- Models for PHP type definitions -*- lexical-binding: t; -*-
;; Copyright (C) 2021-2023 Free Software Foundation, Inc
;; Author: Hugo Thunnissen <[email protected]>
;; Keywords: php, languages, tools, convenience
;; Version: 2.1.0
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;;; Code:
(require 'phpinspect-method-cell)
(require 'phpinspect-property-cell)
(require 'phpinspect-type)
(cl-defstruct (phpinspect-typedef
(:constructor phpinspect-make-typedef-generated)
(:conc-name phpi-typedef-))
"A typedef represents the definition of a PHP type, be it class,
interface, trait or enum."
(read-only-p nil
:type boolean
:documentation
"Whether this typedef is read-only, meaning that its data
should never be changed. Methods and functions that are meant to
manipulate typedef data should become no-ops when this slot has a
non-nil value.")
(retriever nil
:type lambda
:documentaton
"A function that returns typedefs for types
(should accept object of type `phpinspect--type' as argument)")
(methods nil
:type phpinspect-method-collection)
(static-methods nil
:type phpinspect-method-collection)
(properties nil
:type list)
(initial-index nil
:type bool)
(trait-config nil
:documentation
"The configuration of traits that are used in this type")
(-dependencies nil
:documentation "cache for dependencies")
(-dependencies-loaded nil
:documentation "Whether dependencies have
been loaded or not. This slot is externally mutated by projects.")
(method-adder nil
:type phpinspect-method-adder)
(subscribed-types nil
:type list
:documentation "list of phpinspect--type structures.")
(subscribed-to-types nil
:type list
:documentation "list of phpinspect--type structures")
(name nil
:type phpinspect--type)
(declaration nil
:type phpinspect-declaration-p))
(defun phpinspect-make-typedef (type &optional retriever)
(cl-assert (phpinspect--type-p type))
(let ((def (phpinspect-make-typedef-generated
:name type
:retriever retriever
:properties (phpinspect-make-property-collection :home-type type)
:methods (phpinspect-make-method-collection :home-type type)
:static-methods (phpinspect-make-method-collection :home-type type)
:method-adder (phpinspect-make-method-adder))))
def))
(define-inline phpi--typedef-get-foreign-type (def type)
(inline-letevals (def type)
(inline-quote
(when-let ((retriever (phpi-typedef-retriever ,def)))
(funcall retriever ,type)))))
(defmacro phpi--typedef-edit (typedef &rest body)
"Declare intent to edit TYPEDEF in BODY.
Conditionally executes BODY depending on
`phpi-typedef-read-only-p' value."
(declare (indent 1))
(let ((typedef-sym (gensym))
(return-sym (gensym)))
`(let ((,typedef-sym ,typedef)
,return-sym)
(cl-assert
(phpinspect-typedef-p ,typedef-sym) t
"First argument of `phpi--typedef-edit' must be of type `phpinspect-typedef'")
(unless (phpi-typedef-read-only-p ,typedef-sym)
(setq ,return-sym (progn ,@body))
;; Clear cache and unset dependencies loaded.
(setf (phpi-typedef--dependencies ,typedef-sym) nil)
(setf (phpi-typedef--dependencies-loaded ,typedef-sym) nil)
,return-sym))))
(cl-defstruct (phpinspect-method-adder
(:constructor phpinspect-make-method-adder)
(:conc-name phpi-ma-))
"The method adder adds methods to a method-collection, aliasing
and/or overriding methods when appropriate."
(aliases nil
:type alist)
(overrides nil
:type alist))
(defun phpi-ma-get-overrides (ma type)
(alist-get type (phpi-ma-overrides ma) nil nil #'phpinspect--type=))
(defun phpi-ma-get-aliases (ma type)
(alist-get type (phpi-ma-aliases ma) nil nil #'phpinspect--type=))
(defun phpi-ma-set-config (ma config)
"Set type method import config for MA to CONFIG.
This function reconfigures MA to add methods according to the
rules specified in CONFIG. CONFIG is expected to be a list of the
structure returned by `phpinspect--index-trait-use'."
(setf (phpi-ma-aliases ma) nil)
(setf (phpi-ma-overrides ma) nil)
(dolist (typeconf config)
(let ((type (car typeconf))
overrides aliases)
(dolist (setting (cdr typeconf))
(pcase (car setting)
('alias (push (cons (phpinspect-intern-name (cadr setting))
(phpinspect-intern-name (caddr setting)))
aliases))
('override (push (cons (phpinspect-intern-name (cadr setting))
(caddr setting))
overrides))
(_ (error (format "Invalid type configuration instruction %s" (car setting))))))
(push (cons type aliases) (phpi-ma-aliases ma))
(push (cons type overrides) (phpi-ma-overrides ma)))))
(defun phpi-ma-add (ma mcol method &optional overwrite)
(let ((overrides (phpi-ma-get-overrides ma (phpi-method-origin-type method)))
(aliases (phpi-ma-get-aliases ma (phpi-method-origin-type method)))
alias)
;; Can't alias if overriding, can't override if aliasing
(cond ((and overrides (alist-get (phpi-method-name method) overrides))
;; Override basically just means insert and overwrite without checking
(phpi-mcol-add mcol method 'overwrite))
((and aliases (setq alias (alist-get (phpi-method-name method) aliases)))
(let ((copy (phpi-copy-method method)))
;; Rename method to alias, record original name in aliased-from slot
(setf (phpi-method-aliased-from copy) (phpi-method-name method)
(phpi-method-name copy) alias)
(phpi-mcol-add mcol copy overwrite)))
(t
;; Not dealing with aliases or overrides, just add the methods,
;; respecting `overwrite'.
(phpi-mcol-add mcol method overwrite)))))
(defun phpi-typedef-get-methods (def)
(phpi-mcol-list-active (phpi-typedef-methods def)))
(defun phpi-typedef-get-static-methods (def)
(phpi-mcol-list-active (phpi-typedef-static-methods def)))
(cl-defmethod phpi-typedef-get-method ((def phpinspect-typedef) (method-name (head phpinspect-name)))
(phpi-mcol-get-active-method (phpi-typedef-methods def) method-name))
(cl-defmethod phpi-typedef-get-method ((def phpinspect-typedef) (method-name string))
(phpi-typedef-get-method def (phpinspect-intern-name method-name)))
(cl-defmethod phpi-typedef-get-static-method ((def phpinspect-typedef) (method-name (head phpinspect-name)))
(phpi-mcol-get-active-method (phpi-typedef-static-methods def) method-name))
(cl-defmethod phpi-typedef-get-static-method ((def phpinspect-typedef) (method-name string))
(phpi-typedef-get-static-method def (phpinspect-intern-name method-name)))
(cl-defmethod phpi-typedef-set-method ((def phpinspect-typedef) (m phpinspect-method) &optional no-propagate)
(phpi--typedef-edit def
(phpi-ma-add (phpi-typedef-method-adder def) (phpi-typedef-methods def) m 'overwrite)
(unless no-propagate
(phpi-typedef-trigger-subscriber-method-update def m))))
(cl-defmethod phpi-typedef-set-method ((def phpinspect-typedef) (fn phpinspect--function) &optional no-propagate)
(phpi-typedef-set-method def (phpinspect-make-method (phpi-typedef-name def) fn) no-propagate))
(cl-defmethod phpi-typedef-set-static-method ((def phpinspect-typedef) (m phpinspect-method) &optional no-propagate)
(phpi--typedef-edit def
(phpi-ma-add (phpi-typedef-method-adder def) (phpi-typedef-static-methods def) m 'overwrite)
(unless no-propagate
(phpi-typedef-trigger-subscriber-method-update def m 'static))))
(cl-defmethod phpi-typedef-set-static-method ((def phpinspect-typedef) (fn phpinspect--function) &optional no-propagate)
(phpi-typedef-set-static-method def (phpinspect-make-method (phpi-typedef-name def) fn) no-propagate))
(cl-defmethod phpi-typedef-delete-method ((def phpinspect-typedef) (name (head phpinspect-name)))
(phpi--typedef-edit def
(phpi-mcol-delete-for-type
(phpi-typedef-methods def) (phpi-typedef-name def) name)
(phpi-typedef-trigger-subscriber-method-delete def (phpi-typedef-name def) name)
(phpi-mcol-delete-for-type
(phpi-typedef-static-methods def) (phpi-typedef-name def) name)
(phpi-typedef-trigger-subscriber-method-delete def (phpi-typedef-name def) name 'static)))
(cl-defmethod phpi-typedef-delete-method ((def phpinspect-typedef) (name string))
(phpi-typedef-delete-method def (phpinspect-intern-name name)))
(cl-defmethod phpi-typedef-delete-method ((def phpinspect-typedef) (fn phpinspect--function))
(phpi-typedef-delete-method def (phpinspect--function-name fn)))
(defun phpi-typedef-set-trait-config (def config)
"Configures CONFIG for typedef DEF.
Note: this function returns all types found in CONFIG."
(phpi--typedef-edit def
(let ((existing-config (phpi-typedef-trait-config def))
types)
;; A configuration that is exactly the same doesn't need to be applied twice
(unless (equal config existing-config)
(when existing-config
;; This can probably be made more sophisticated by only applying the
;; difference, but just deleting all incorporated methods will do for
;; an initial implementation.
(dolist (use existing-config)
(when-let ((fd (phpi--typedef-get-foreign-type def (car use))))
(phpi-typedef-unsubscribe-from-foreign-typedef def fd))))
;; Apply new config
(phpi-ma-set-config (phpi-typedef-method-adder def) config))
(dolist (cons config)
(push (car cons) types))
(setf (phpi-typedef-trait-config def) config)
;; return all types that were in config
types)))
(defun phpi-typedef-add-foreign-members (def foreign-def)
"Add methods and properties to DEF from FOREIGN-DEF."
(phpi--typedef-edit def
(when foreign-def
(let ((ma (phpi-typedef-method-adder def))
(methods (phpi-typedef-methods def))
(st-methods (phpi-typedef-static-methods def))
(props (phpi-typedef-properties def)))
(dolist (method (phpi-typedef-get-methods foreign-def))
(phpi-ma-add ma methods method))
(dolist (method (phpi-typedef-get-static-methods foreign-def))
(phpi-ma-add ma st-methods method))
(dolist (prop (phpi-pcol-list-active (phpi-typedef-properties foreign-def)))
(phpi-pcol-add props prop))))))
(defun phpi-typedef-subscribe-to-foreign-typedef (def foreign-def)
(phpi--typedef-edit def
(push (phpi-typedef-name def) (phpi-typedef-subscribed-types foreign-def))
(push (phpi-typedef-name foreign-def) (phpi-typedef-subscribed-to-types def))))
(defun phpi-typedef-unsubscribe-from-foreign-typedef (def foreign-def)
"Unsubscribe DEF from changes in FOREIGN-DEF.
This undoes any links and data sharing between this type and any
extended classes, used traits or implemented interfaces."
(phpi--typedef-edit def
(setf (phpi-typedef-subscribed-types foreign-def)
(cl-remove (phpi-typedef-name def) (phpi-typedef-subscribed-types foreign-def)
:test #'phpinspect--type=))
(setf (phpi-typedef-subscribed-to-types def)
(cl-remove (phpi-typedef-name foreign-def) (phpi-typedef-subscribed-types def)
:test #'phpinspect--type=))
;; Remove all members of foreign typedef
(let ((ft (phpi-typedef-name foreign-def)))
(phpi-mcol-delete-for-type (phpi-typedef-methods def) ft)
(phpi-mcol-delete-for-type (phpi-typedef-static-methods def) ft)
(phpi-pcol-delete-for-type (phpi-typedef-properties def) ft))))
(defun phpi-typedef-trigger-subscriber-update (def)
"Incorporate DEF into subscribed typedefs."
(dolist (type (phpi-typedef-subscribed-types def))
(when-let ((foreign-def (phpi--typedef-get-foreign-type def type)))
(phpi-typedef-add-foreign-members foreign-def def)
;; Notify subscribers of upstream changes
(phpi-typedef-trigger-subscriber-update foreign-def))))
(defun phpi-typedef-trigger-subscriber-method-update (def method &optional static)
"Update downstream subscribers of DEF by setting METHOD.
If STATIC is non-nil, updates static method."
(dolist (type (phpi-typedef-subscribed-types def))
(when-let ((foreign-def (phpi--typedef-get-foreign-type def type)))
(if static
(phpi-typedef-set-static-method foreign-def method)
(phpi-typedef-set-method foreign-def method)))))
(defun phpi-typedef-trigger-subscriber-property-update (def property)
"Update downstream subscribers of DEF by setting PROPERTY."
(dolist (type (phpi-typedef-subscribed-types def))
(when-let ((foreign-def (phpi--typedef-get-foreign-type def type)))
(phpi-typedef-set-property foreign-def property))))
(defun phpi-typedef-trigger-subscriber-property-delete (def origin-type property-name)
"Update subscribers of DEF by deleting PROPERTY-NAME of origin TYPE."
(dolist (subtype (phpi-typedef-subscribed-types def))
(when-let ((foreign-def (phpi--typedef-get-foreign-type def subtype)))
(phpi--typedef-edit foreign-def
(phpi-pcol-delete-for-type
(phpi-typedef-properties foreign-def) origin-type property-name)
(phpi-typedef-trigger-subscriber-property-delete foreign-def origin-type property-name)))))
(defun phpi-typedef-trigger-subscriber-method-delete (def type method-name &optional static)
"Update subscribers of DEF by deleting METHOD-NAME of origin TYPE."
(dolist (subtype (phpi-typedef-subscribed-types def))
(when-let ((foreign-def (phpi--typedef-get-foreign-type def subtype)))
(phpi--typedef-edit foreign-def
(phpi-mcol-delete-for-type
(if static (phpi-typedef-static-methods foreign-def) (phpi-typedef-methods foreign-def))
type method-name))
(phpi-typedef-trigger-subscriber-method-delete foreign-def type method-name static))))
(defun phpi-typedef-set-name (def type)
"Set the TYPE name of DEF.
TYPE must be a structure of type `phpinspect--type'."
(phpi--typedef-edit def
(cl-assert (phpinspect--type-p type))
(let ((existing (phpi-typedef-name def)))
;; Only update if type name is actually different
(unless (phpinspect--type= existing type)
(setf (phpi-typedef-name def) type)
(phpi-mcol-set-home-type (phpi-typedef-methods def) type)
(phpi-mcol-set-home-type (phpi-typedef-static-methods def) type)
(phpi-pcol-set-home-type (phpi-typedef-properties def) type)))))
(defun phpi-typedef-update-declaration (def declaration imports namespace-name trait-config parent)
"Update declaration of DEF.
DECLARATION must be a token of type `phpinspect-declaration-p`.
IMPORTS must be an alist of imported types as returned by
`phpinspect--uses-to-types'.
NAMESPACE-NAME must be a string containing the name of the
namespace enclosing DEF.
TRAIT-CONFIG must be a trait configuration as returned by
`phpinspect--index-trait-use'."
(phpi--typedef-edit def
(pcase-let ((`(,type ,extends ,implements ,_used-types)
(phpinspect--index-class-declaration
declaration (phpinspect--make-type-resolver
(phpinspect--uses-to-types imports) nil namespace-name)
parent)))
(phpi-typedef-set-name def type)
(setf (phpi-typedef-declaration def) declaration)
(phpi-typedef-update-extensions
def `(,@extends ,@implements ,@(phpi-typedef-set-trait-config def trait-config))))))
(defun phpi-typedef-update-extensions (def extensions)
"Sets extended types of DEF to EXTENSIONS.
EXTENSIONS must be a list of structures of the type
`phpinspect--type'. These are used to fetch typedefs belonging to
them, which are then incorporated into DEF's properties."
(phpi--typedef-edit def
(when-let ((subscriptions (phpi-typedef-subscribed-to-types def)))
(dolist (sub subscriptions)
(unless (cl-member sub extensions :test #'phpinspect--type=)
;; No longer an extended type, unsubscribe
(when-let ((fd (phpi--typedef-get-foreign-type def sub)))
(phpi-typedef-unsubscribe-from-foreign-typedef def fd))))
(dolist (ext extensions)
(when (cl-member ext subscriptions :test #'phpinspect--type=)
;; Already subscribed, no refresh needed
(delq ext extensions))))
(when-let ((foreign-defs
(seq-filter
#'phpinspect-typedef-p
(mapcar
(lambda (class-name)
(phpi--typedef-get-foreign-type def class-name))
extensions))))
(dolist (fd foreign-defs)
(phpi-typedef-add-foreign-members def fd)
(phpi-typedef-subscribe-to-foreign-typedef def fd)))))
(cl-defmethod phpi-typedef-set-index ((def phpinspect-typedef)
(index (head phpinspect--indexed-class)))
"Update all of DEF's properties using the contents of INDEX."
(phpi--typedef-edit def
(setf (phpi-typedef-declaration def) (alist-get 'declaration index))
(let ((ma (phpi-typedef-method-adder def))
(mcol (phpi-typedef-methods def))
(stmcol (phpi-typedef-static-methods def))
(home-type (phpi-typedef-name def))
(pcol (phpi-typedef-properties def)))
;; Override methods when class seems syntactically correct (has balanced braces)
(when (alist-get 'complete index)
;; Delete all known own methods and properties
(phpi-mcol-delete-for-type mcol home-type)
(phpi-mcol-delete-for-type stmcol home-type)
(phpi-pcol-delete-for-type pcol home-type))
(dolist (method (alist-get 'methods index))
(phpi-ma-add ma mcol (phpinspect-make-method home-type method) 'overwrite))
(dolist (method (alist-get 'static-methods index))
(phpi-ma-add ma stmcol (phpinspect-make-method home-type method) 'overwrite))
(setf (phpi-typedef-initial-index def) t)
(let-alist index
(dolist (var (append .variables .constants .static-variables))
(phpi-pcol-add pcol (phpinspect-make-property home-type var))))
(phpi-typedef-update-extensions
def `(,@(alist-get 'implements index)
,@(alist-get 'extends index)
,@(phpi-typedef-set-trait-config def (alist-get 'trait-config index))))
(phpi-typedef-trigger-subscriber-update def))))
(cl-defmethod phpi-typedef-get-method-return-type
((def phpinspect-typedef) (method-name (head phpinspect-name)) &optional tryhard)
(phpi-mcol-get-return-type (phpi-typedef-methods def) method-name tryhard))
(cl-defmethod phpi-typedef-get-static-method-return-type
((def phpinspect-typedef) (method-name (head phpinspect-name)) &optional tryhard)
(phpi-mcol-get-return-type (phpi-typedef-static-methods def) method-name tryhard))
(cl-defmethod phpi-typedef-set-property ((def phpinspect-typedef) (prop phpinspect-property))
(phpi--typedef-edit def
(phpi-pcol-add (phpi-typedef-properties def) prop)
(phpi-typedef-trigger-subscriber-property-update def prop)))
(cl-defmethod phpi-typedef-set-property ((def phpinspect-typedef)
(var phpinspect--variable))
(phpi-typedef-set-property
def (phpinspect-make-property (phpi-typedef-name def) var)))
(cl-defmethod phpi-typedef-delete-property ((def phpinspect-typedef)
(name (head phpinspect-name))
&optional origin-type)
(phpi--typedef-edit def
(phpi-pcol-delete-for-type
(phpi-typedef-properties def) (phpi-typedef-name def) name)
(phpi-typedef-trigger-subscriber-property-delete
def (or origin-type (phpi-typedef-name def)) name)))
(cl-defmethod phpi-typedef-delete-property ((def phpinspect-typedef)
(var phpinspect--variable))
(phpi-typedef-delete-property
def (phpinspect-intern-name (phpinspect--variable-name var))))
(cl-defmethod phpi-typedef-get-properties ((def phpinspect-typedef))
(seq-filter #'phpi-prop-vanilla-p
(phpi-pcol-list-active (phpi-typedef-properties def))))
(cl-defmethod phpi-typedef-get-static-properties ((def phpinspect-typedef))
(seq-filter #'phpi-prop-static-p
(phpi-pcol-list-active (phpi-typedef-properties def))))
(cl-defmethod phpi-typedef-get-constants ((def phpinspect-typedef))
(seq-filter #'phpi-prop-const-p
(phpi-pcol-list-active (phpi-typedef-properties def))))
(cl-defmethod phpi-typedef-get-property
((def phpinspect-typedef) (prop-name string))
(phpi-pcol-get-active-property
(phpi-typedef-properties def) (phpinspect-intern-name prop-name)))
(defun phpi-typedef-get-dependencies (def)
"Gets types that DEF directly depends on.
Return value is a list if structures of the type
`phpinspect--type'."
(with-memoization (phpi-typedef--dependencies def)
(let ((deps (phpi-typedef-subscribed-to-types def)))
(dolist (method (phpi-typedef-get-methods def))
(when (phpi-method-return-type method)
(push (phpi-method-return-type method) deps)))
(dolist (method (phpi-typedef-get-static-methods def))
(when (phpi-method-return-type method)
(push (phpi-method-return-type method) deps)))
(dolist (prop (phpi-pcol-list-active (phpi-typedef-properties def)))
(when (phpi-prop-type prop)
(push (phpi-prop-type prop) deps)))
(phpinspect--types-uniq deps (phpi-typedef-name def)))))
(provide 'phpinspect-typedef)
;;; phpinspect-typedef.el ends here