Repository navigation
Expand file tree
/
Copy pathdefault-methods.lisp
More file actions
564 lines (501 loc) · 24 KB
/
Copy pathdefault-methods.lisp
File metadata and controls
564 lines (501 loc) · 24 KB
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
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
(cl:in-package #:clostrum-implementation)
(defmethod env:merge-types (client type1 type2)
(declare (ignore client))
;; by default, assume that they are just type expanders.
(cond ((eql type1 't) type2)
((eql type2 't) type1)
(t `(and ,type1 ,type2))))
(defmethod env:merge-optimize (client new-optimize old-optimize)
(declare (ignore client))
;; by default, assume only standard qualities, and that the optimizes are raw.
(loop for quality in '(speed space safety debug compilation-speed)
if (member quality new-optimize)
collect `(,quality ,3)
else if (assoc quality new-optimize)
collect it
else if (member quality old-optimize)
collect `(,quality ,3)
else if (assoc quality old-optimize)
collect it))
;;; Operators
(defun find-operator-cell (client environment operator-name)
"Find an operator cell if it exists, or return NIL. Internal."
(or (sys:operator-cell client environment operator-name)
(let ((parent (env:parent client environment)))
(if parent
(find-operator-cell client parent operator-name)
nil))))
(defmethod env:operator-status (client environment operator-name)
(or (sys:operator-status client environment operator-name)
(let ((parent (env:parent client environment)))
(if parent
(env:operator-status client parent operator-name)
nil))))
(defmethod env:ensure-operator-cell (client environment operator-name)
(or (sys:operator-cell client environment operator-name)
(let* ((parent (env:parent client environment))
(parent-cell
(if parent
(find-operator-cell client parent operator-name)
nil))
(new-cell
(sys:ensure-operator-cell client environment operator-name)))
(when parent-cell
;; Copy in the old value.
(when (sys:operator-cell-boundp client parent-cell)
(setf (sys:operator-cell-value client new-cell)
(sys:operator-cell-value client parent-cell))))
new-cell)))
(defmethod env:fdefinition (client environment operator-name)
(let ((cell (find-operator-cell client environment operator-name)))
(if (and cell (sys:operator-cell-boundp client cell))
(sys:operator-cell-value client cell)
(error 'undefined-function :name operator-name))))
(defmethod (setf env:fdefinition)
(new client (environment env:run-time-environment) operator-name)
(let ((cell (env:ensure-operator-cell client environment operator-name)))
(setf (sys:operator-status client environment operator-name) :function
(sys:operator-cell-value client cell) new)))
(defmethod env:fboundp (client environment operator-name)
(let ((cell (find-operator-cell client environment operator-name)))
(and cell (sys:operator-cell-boundp client cell))))
(defmethod env:fmakunbound
(client (environment env:run-time-environment) operator-name)
;; NOTE: We do not forbid fmakunbound of special operators. If the client
;; wants to forbid that, it is assumed they will roll that into a general
;; package lock mechanism that forbids undefining standard functions and
;; macros as well, and that's out of scope for Clostrum.
;; Similar considerations apply to (setf fdefinition), etc.
(setf (sys:operator-status client environment operator-name) nil)
;; We do ENSURE-OPERATOR-CELL so that fmakunbound cannot affect
;; any ancestral environments.
(let ((cell (env:ensure-operator-cell client environment operator-name)))
(sys:operator-cell-makunbound client cell))
operator-name)
(defmethod env:macro-function (client environment operator-name)
(if (eq (env:operator-status client environment operator-name) :macro)
(let ((cell (find-operator-cell client environment operator-name)))
(if (and cell (sys:operator-cell-boundp client cell))
(sys:operator-cell-value client cell)
nil))
nil))
(defmethod (setf env:macro-function) (new client environment operator-name)
(let ((cell (env:ensure-operator-cell client environment operator-name)))
(setf (sys:operator-status client environment operator-name) :macro
(sys:operator-cell-value client cell) new)))
(defmethod env:compiler-macro-function (client environment operator-name)
(or (sys:compiler-macro-function client environment operator-name)
(let ((parent (env:parent client environment)))
(if parent
(env:compiler-macro-function client parent operator-name)
nil))))
(defmethod (setf env:compiler-macro-function) (new client environment operator-name)
(setf (sys:compiler-macro-function client environment operator-name) new))
(defmethod env:special-operator-p (client environment operator-name)
(eq (env:operator-status client environment operator-name) :special-operator))
(defmethod env:make-special-operator (client environment operator-name new)
(let ((cell (env:ensure-operator-cell client environment operator-name)))
(setf (sys:operator-cell-value client cell) new
(sys:operator-status client environment operator-name) :special-operator)
operator-name))
(defmethod env:note-function (client environment operator-name)
(let ((status (sys:operator-status client environment operator-name)))
(ecase status
((nil)
(setf (sys:operator-status client environment operator-name) :function))
((:function))
((:macro :special-operator)
(error 'env:attempt-to-note-operator-as-function
:name operator-name :status status)))))
(defmethod env:operator-ftype (client environment operator-name)
(let ((parent (env:parent client environment))
(ftype (sys:operator-ftype client environment operator-name)))
(if parent
(env:merge-types client ftype (env:operator-ftype client parent operator-name))
ftype)))
(defmethod (setf env:operator-ftype) (new client environment operator-name)
(let ((status (env:operator-status client environment operator-name)))
(if (eq :function status)
(setf (sys:operator-ftype client environment operator-name) new)
(error 'env:attempt-to-set-ftype-of-non-function
:name operator-name :status status))))
(defmethod env:operator-inline (client environment operator-name)
(if (sys:operator-inline-known-p client environment operator-name)
(sys:operator-inline client environment operator-name)
(let ((parent (env:parent client environment)))
(if parent
(env:operator-inline client parent operator-name)
nil))))
(defmethod (setf env:operator-inline) (new client environment operator-name)
(setf (sys:operator-inline client environment operator-name) new))
(defmethod env:operator-inline-data (client environment operator-name)
(or (sys:operator-inline-data client environment operator-name)
(let ((parent (env:parent client environment)))
(if parent
(env:operator-inline-data client parent operator-name)
nil))))
(defmethod (setf env:operator-inline-data) (new client environment operator-name)
(setf (sys:operator-inline-data client environment operator-name) new))
(defmethod env:setf-expander (client (environment env:compilation-environment) operator-name)
(let ((parent (env:parent client environment)))
(if parent
(env:setf-expander client parent operator-name)
nil)))
(defmethod env:setf-expander (client (environment env:run-time-environment) operator-name)
(or (sys:setf-expander client environment operator-name)
(let ((parent (env:parent client environment)))
(if parent
(env:setf-expander client parent operator-name)
nil))))
(defmethod (setf env:setf-expander) (new client environment operator-name)
(case (env:operator-status client environment operator-name)
;; CLHS says DEFINE-SETF-EXPANDER only works on function and macro names, but
;; we loosen that restriction a bit so that a setf expander can be defined for
;; THE. If an environment wants to restrict DEFINE-SETF-EXPANDER it can do so
;; just as well in the definition of DEFINE-SETF-EXPANDER, not the primitive
;; accessor here.
((:function :macro :special-operator)
(setf (sys:setf-expander client environment operator-name) new))
(otherwise
(error 'env:attempt-to-define-a-setf-expander-of-non-existing-function-or-macro
:name operator-name))))
;;; Variables
(defun find-variable-cell (client environment variable-name)
"Find an variable cell if it exists, or return NIL. Internal."
(or (sys:variable-cell client environment variable-name)
(let ((parent (env:parent client environment)))
(if parent
(find-variable-cell client parent variable-name)
nil))))
(defmethod env:variable-status (client environment variable-name)
(or (sys:variable-status client environment variable-name)
(let ((parent (env:parent client environment)))
(if parent
(env:variable-status client parent variable-name)
nil))))
(defmethod env:ensure-variable-cell (client environment variable-name)
(or (sys:variable-cell client environment variable-name)
(let* ((parent (env:parent client environment))
(parent-cell
(if parent
(find-variable-cell client parent variable-name)
nil))
(new-cell
(sys:ensure-variable-cell client environment variable-name)))
(when parent-cell
;; Copy in the old value.
(when (sys:variable-cell-boundp client parent-cell)
(setf (sys:variable-cell-value client new-cell)
(sys:variable-cell-value client parent-cell))))
new-cell)))
(defmethod env:symbol-value (client environment variable-name)
(let ((cell (find-variable-cell client environment variable-name)))
(if (and cell (sys:variable-cell-boundp client cell))
(sys:variable-cell-value client cell)
(error 'unbound-variable :name variable-name))))
(defmethod (setf env:symbol-value)
(new client (environment env:run-time-environment) variable-name)
(if (eq (env:variable-status client environment variable-name) :constant)
(error 'env:attempt-to-set-constant-value :name variable-name)
(let ((cell (env:ensure-variable-cell client environment variable-name)))
(setf (sys:variable-cell-value client cell) new))))
(defmethod env:boundp (client environment variable-name)
(let ((cell (sys:variable-cell client environment variable-name)))
(and cell (sys:variable-cell-boundp client cell))))
(defmethod env:makunbound (client (environment env:run-time-environment) variable-name)
(sys:variable-cell-makunbound
client (env:ensure-variable-cell client environment variable-name)))
(defmethod env:variable-type (client environment variable-name)
(let ((parent (env:parent client environment))
(type (sys:variable-type client environment variable-name)))
(if parent
(env:merge-types client type (env:variable-type client parent variable-name))
type)))
(defmethod (setf env:variable-type) (new client environment variable-name)
(setf (sys:variable-type client environment variable-name) new))
(defmethod env:make-variable (client environment variable-name &optional value)
(declare (ignore value))
(ecase (env:variable-status client environment variable-name)
((nil)
(setf (sys:variable-status client environment variable-name) :special))
((:special))
((:constant)
(error 'env:attempt-to-define-special-variable-for-existing-constant
:name variable-name))
((:symbol-macro)
(error 'env:attempt-to-define-special-variable-for-existing-symbol-macro
:name variable-name))))
(defmethod env:make-variable :after
(client (environment env:run-time-environment) variable-name
&optional (value nil valuep))
(when valuep
(let ((cell (env:ensure-variable-cell client environment variable-name)))
(unless (sys:variable-cell-boundp client cell)
(setf (sys:variable-cell-value client cell) value)))))
(defmethod env:make-parameter
(client (environment env:run-time-environment) variable-name new)
(ecase (env:variable-status client environment variable-name)
((nil)
(setf (sys:variable-cell-value
client (env:ensure-variable-cell client environment variable-name))
new
(sys:variable-status client environment variable-name)
:special))
((:special)
(let ((cell (env:ensure-variable-cell client environment variable-name)))
(setf (sys:variable-cell-value client cell) new)))
((:constant)
(error 'env:attempt-to-define-special-variable-for-existing-constant
:name variable-name))
((:symbol-macro)
(error 'env:attempt-to-define-special-variable-for-existing-symbol-macro
:name variable-name))))
(defmethod env:make-constant (client environment variable-name new)
(ecase (env:variable-status client environment variable-name)
((nil) ; undefined: this is a new constant
(setf (sys:variable-status client environment variable-name)
:constant
(sys:variable-cell-value
client (env:ensure-variable-cell client environment variable-name))
new))
((:constant)
(let ((old
;; The cell must be bound, as constants always are.
(sys:variable-cell-value
client (find-variable-cell client environment variable-name))))
(unless (eql old new)
;; TODO: Restarts?
(error 'env:attempt-to-redefine-constant-incompatibly
:name variable-name :old old :new new))
new))
((:special)
(error 'env:attempt-to-define-constant-for-existing-special-variable
:name variable-name))
((:symbol-macro)
(error 'env:attempt-to-define-constant-for-existing-symbol-macro
:name variable-name))))
(defun env:variable-macro-expander (client environment variable-name)
(labels ((%variable-macro-expander (environment)
(or (sys:variable-macro-expander client environment variable-name)
;; we already checked the status,
;; so we know SOMETHING defines this expander.
(%variable-macro-expander (env:parent client environment)))))
(ecase (env:variable-status client environment variable-name)
((nil :special :constant) nil)
((:symbol-macro) (%variable-macro-expander environment)))))
(defun %make-symbol-macro-expander (expansion)
(lambda (form env)
(declare (ignore form env))
expansion))
(defmethod env:make-symbol-macro (client environment variable-name expansion)
(ecase (env:variable-status client environment variable-name)
((nil :symbol-macro)
(setf (sys:variable-macro-expander client environment variable-name)
(%make-symbol-macro-expander expansion)
(sys:variable-status client environment variable-name)
:symbol-macro))
((:special)
(error 'env:attempt-to-define-symbol-macro-for-existing-special-variable
:name variable-name))
((:constant)
(error 'env:attempt-to-define-symbol-macro-for-existing-constant
:name variable-name))))
(defmethod env:symbol-plist (client environment symbol)
(if (sys:symbol-plist-known-p client environment symbol)
(sys:symbol-plist client environment symbol)
(labels ((%plist (env)
(if (sys:symbol-plist-known-p client env symbol)
(values (sys:symbol-plist client env symbol) t)
(let ((parent (env:parent client env)))
(if parent
(%plist parent)
(values nil nil))))))
(let ((parent (env:parent client environment)))
(if parent
(%plist parent)
nil)))))
(defmethod (setf env:symbol-plist)
(new client (environment env:run-time-environment) symbol)
(setf (sys:symbol-plist client environment symbol) new))
;;; Types and classes
(defun find-type-cell (client environment type-name)
"Find a type cell if it exists, or return NIL. Internal."
(or (sys:type-cell client environment type-name)
(let ((parent (env:parent client environment)))
(if parent
(find-type-cell client parent type-name)
nil))))
(defmethod env:ensure-type-cell (client environment type-name)
(or (sys:type-cell client environment type-name)
(let* ((parent (env:parent client environment))
(parent-cell
(if parent
(find-type-cell client parent type-name)
nil))
(new-cell
(sys:ensure-type-cell client environment type-name)))
(when parent-cell
;; Copy in the old value.
(when (sys:type-cell-boundp client parent-cell)
(setf (sys:type-cell-value client new-cell)
(sys:type-cell-value client parent-cell))))
new-cell)))
(defmethod env:find-class (client environment class-name &optional (errorp t))
(let ((cell (find-type-cell client environment class-name)))
(cond ((sys:type-cell-boundp client cell)
(sys:type-cell-value client cell))
(errorp (error 'env:undefined-class :name class-name))
(t nil))))
(defmethod (setf env:find-class)
(new client environment class-name &optional errorp)
(declare (ignore errorp))
(setf (sys:type-expander client environment class-name) nil)
(cond ((null new)
(sys:type-cell-makunbound
client (env:ensure-type-cell client environment class-name)))
(t
(setf (sys:type-cell-value
client (env:ensure-type-cell client environment class-name))
new)))
new)
(defmethod env:type-expander (client environment type-name)
(let ((cell (sys:type-cell client environment type-name)))
(cond ((and cell (sys:type-cell-boundp client cell)) nil)
((sys:type-expander client environment type-name))
(t (let ((parent (env:parent client environment)))
(if parent
(env:type-expander client parent type-name)
nil))))))
(defmethod (setf env:type-expander) (expander client environment type-name)
(setf (sys:type-expander client environment type-name) expander)
type-name)
(defmethod env:type-expand-1 (client environment (type-specifier symbol))
(let ((expander (env:type-expander client environment type-specifier)))
(if (null expander)
(values type-specifier nil)
(values (funcall expander type-specifier environment) t))))
(defmethod env:type-expand-1 (client environment (type-specifier cons))
(let ((operator (car type-specifier)))
(if (symbolp operator)
(let ((expander (env:type-expander client environment operator)))
(if (null expander)
(values type-specifier nil)
(values (funcall expander type-specifier environment) t)))
(values type-specifier nil))))
(defmethod env:type-expand-1 (client environment (type-specifier class))
(declare (ignore client environment))
type-specifier)
(defmethod env:type-expand (client environment type-specifier)
(loop with ever-expanded = nil
do (multiple-value-bind (expansion expandedp)
(env:type-expand-1 client environment type-specifier)
(if expandedp
(setf ever-expanded t type-specifier expansion)
(return (values type-specifier ever-expanded))))))
;;; Packages
(defmethod env:find-package (client (environment env:run-time-environment) name)
(or (sys:find-package client environment name)
(let ((parent (env:parent client environment)))
(if parent
(env:find-package client parent name)
nil))))
(defmethod env:find-package
(client (environment env:compilation-environment) name)
(let ((parent (env:parent client environment)))
(if parent
(env:find-package client parent name)
nil)))
(defmethod (setf env:find-package)
(new client (environment env:run-time-environment) name)
(setf (sys:find-package client environment name) new))
(defmethod env:package-name (client (environment env:run-time-environment) package)
(or (sys:package-name client environment package)
(let ((parent (env:parent client environment)))
(if parent
(setf (sys:package-name client environment package)
(env:package-name client parent package))
nil))))
(defmethod env:package-name (client (environment env:compilation-environment) package)
(let ((parent (env:parent client environment)))
(if parent
(env:package-name client parent package)
nil)))
(defmethod (setf env:package-name)
(new client (environment env:run-time-environment) package)
(setf (sys:package-name client environment package) new))
(defmethod env:map-all-packages (client (env env:run-time-environment) function)
(let ((parent (env:parent client env)))
(if parent
;; In order to get everything including parents, we gather up a list and then
;; map over that. Kind of inefficient.
(let ((packages nil))
(declare (dynamic-extent packages))
(sys:map-all-packages client env (lambda (p) (push p packages)))
(env:map-all-packages client parent (lambda (p) (pushnew p packages)))
(mapc function packages))
;; Easy case.
(sys:map-all-packages client env function))))
(defmethod env:map-all-packages (client (env env:compilation-environment) function)
(let ((parent (env:parent client env)))
(when parent
(env:map-all-packages client parent function))))
(defmethod env:package-names (client (env env:run-time-environment) package)
(append
(sys:package-names client env package)
(let ((parent (env:parent client env)))
(if parent
(env:package-names client parent package)
nil))))
(defmethod env:package-names (client (env env:compilation-environment) package)
(let ((parent (env:parent client env)))
(if parent
(env:package-names client parent package)
nil)))
;;; Optimize
(defmethod env:optimize (client environment) (sys:optimize client environment))
(defmethod env:proclaim-optimize (client environment optimize)
(setf (sys:optimize client environment)
(env:merge-optimize client optimize (env:optimize client environment))))
;;; Combination
(defmethod env:macroexpand-1 (client environment (form symbol))
(let ((expander (env:variable-macro-expander client environment form)))
(if expander
(values (funcall *macroexpand-hook* expander form environment) t)
(values form nil))))
(defmethod env:macroexpand-1 (client environment (form cons))
(let ((operator (car form)))
(if (symbolp operator)
(let ((expander (env:macro-function client environment operator)))
(if expander
(values (funcall *macroexpand-hook* expander form environment) t)
(values form nil)))
(values form nil))))
(defmethod env:macroexpand-1 (client environment (form t))
(declare (ignore client environment))
(values form nil))
(defmethod env:macroexpand (client environment form)
(loop with ever-expanded = nil
do (multiple-value-bind (expansion expandedp)
(env:macroexpand-1 client environment form)
(if expandedp
(setf ever-expanded t form expansion)
(return (values form ever-expanded))))))
(defmethod env:constantp (client environment (form symbol))
(case (env:variable-status client environment form)
((:constant) t)
((:symbol-macro)
(env:constantp
client environment (env:macroexpand-1 client environment form)))
(otherwise nil)))
(defmethod env:constantp (client environment (form cons))
(declare (ignore client environment))
;; This method is kind of awkward: A client must take care of QUOTE itself
;; to use this function for cl:constantp. Clostrum does not assume that QUOTE
;; always has its standard meaning, and because it imposes no meaning on
;; special operator values (returned by fdefinition), it has no way to check
;; that QUOTE has its standard meaning.
nil)
(defmethod env:constantp (client environment (form t))
(declare (ignore client environment))
;; not a symbol or cons. therefore the form is self-evaluating.
t)