-
Notifications
You must be signed in to change notification settings - Fork 4
/
default-augmentation-classes.lisp
376 lines (310 loc) · 13.2 KB
/
default-augmentation-classes.lisp
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
(cl:in-package #:cleavir-environment)
;;;; This file contains classes that can be used to augment an
;;;; environment with local information. Existing implementations can
;;;; not use these classes, because the existing macro expander must
;;;; know how to deal with them. They can be used in new
;;;; implementations where the creator does not have any particular
;;;; opinion about how to represent locally augmented environments.
;;;;
;;;; An alternative strategy for an existing implementation is to
;;;; modify its implementation of MACROEXPAND-1 so that it can handle
;;;; both the native representation of augmented environments, and
;;;; this one supplied by Cleavir.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Class ENTRY.
;;;
;;; This class is the root of all locally augmented environments. We
;;; represent a locally augmented environment as a chain, i.e., each
;;; instance has a NEXT slot that refers to the less specific
;;; environment. The chain ends with a reference to an object
;;; representing the global environment. Implementations that do not
;;; have first-class global environments must still create a dummy
;;; class that represents their particular global environment.
(defclass entry ()
((%next :initarg :next :reader next)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; LEXICAL-VARIABLE.
;;; This class is used to augment an environment with a lexical
;;; variable introduced by LET or LET*. Client code can supply an
;;; IDENTITY object that is used to distinguish between different
;;; lexical variables with the same name.
(defclass lexical-variable (entry)
((%name :initarg :name :reader name)
(%identity :initarg :identity :reader identity)))
(defmethod add-lexical-variable
(environment symbol &optional (identity (gensym)))
(make-instance 'lexical-variable
:next environment
:name symbol
:identity identity))
(defmethod print-object ((object lexical-variable) stream)
(print-unreadable-object (object stream :type t :identity t)
(format stream "~s ~s" (name object) (identity object))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; SPECIAL-VARIABLE.
;;; This class is used to augment an environment with a special
;;; variable. This class is only used when a binding of a special
;;; variable is seen, and that variable is not globally special. By
;;; `globally special', we mean that a proclamation has been used
;;; directly or indirectly to proclaim the variable as special.
;;;
;;; Whereas a lexical variable needs some kind of identity in order to
;;; distinguish between different lexical variables with the same
;;; name, no such identity is needed for special variables, simply
;;; because there is only one special variable with a particular name.
;;; Thus, the name of the special variable is sufficient to determine
;;; which special variable is meant.
(defclass special-variable (entry)
((%name :initarg :name :reader name)))
(defmethod add-special-variable (environment symbol)
(make-instance 'special-variable
:next environment
:name symbol))
(defmethod print-object ((object special-variable) stream)
(print-unreadable-object (object stream :type t :identity t)
(format stream "~s" (name object))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; SYMBOL-MACRO.
;;; This class is used to augment an environment with a symbol macro.
;;; We use the expansion of the symbol macro rather than expander
;;; function.
(defclass symbol-macro (entry)
((%name :initarg :name :reader name)
(%expansion :initarg :expansion :reader expansion)))
(defmethod add-local-symbol-macro (environment symbol expansion)
(make-instance 'symbol-macro
:next environment
:name symbol
:expansion expansion))
(defmethod print-object ((object symbol-macro) stream)
(print-unreadable-object (object stream :type t :identity t)
(format stream "~s ~s" (name object) (expansion object))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; FUNCTION.
;;; This class is used to augment an environment with a local function
;;; introduced by FLET or LABELS. Client code can supply an IDENTITY
;;; object that is used to distinguish between different local
;;; functions with the same name.
(defclass function (entry)
((%name :initarg :name :reader name)
(%identity :initarg :identity :reader identity)))
(defmethod add-local-function
(environment function-name &optional (identity (gensym)))
(make-instance 'function
:next environment
:name function-name
:identity identity))
(defmethod print-object ((object function) stream)
(print-unreadable-object (object stream :type t :identity t)
(format stream "~s ~s" (name object) (identity object))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; MACRO.
;;; This class is used to augment an environment with a local macro
;;; introduced by MACROLET.
(defclass macro (entry)
((%name :initarg :name :reader name)
(%expander :initarg :expander :reader expander)))
(defmethod add-local-macro (environment symbol expander)
(make-instance 'macro
:next environment
:name symbol
:expander expander))
(defmethod print-object ((object macro) stream)
(print-unreadable-object (object stream :type t :identity t)
(format stream "~s" (name object))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; BLOCK.
;;; This class is used to augment an environment with a block
;;; introduced by BLOCK. Client code can supply an IDENTITY object
;;; that is used to distinguish between different blocks with the same
;;; name.
(defclass block (entry)
((%name :initarg :name :reader name)
(%identity :initarg :identity :reader identity)))
(defmethod add-block (environment symbol &optional (identity (gensym)))
(make-instance 'block
:next environment
:name symbol
:identity identity))
(defmethod print-object ((object block) stream)
(print-unreadable-object (object stream :type t :identity t)
(format stream "~s ~s" (name object) (identity object))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; TAG.
;;; This class is used to augment an environment with a tag introduced
;;; by TAGBODY for each go tag. Client code can supply an IDENTITY
;;; object that is used to distinguish between different tags with
;;; the same name.
(defclass tag (entry)
((%name :initarg :name :reader name)
(%identity :initarg :identity :reader identity)))
(defmethod add-tag (environment symbol &optional (identity (gensym)))
(make-instance 'tag
:next environment
:name symbol
:identity identity))
(defmethod print-object ((object tag) stream)
(print-unreadable-object (object stream :type t :identity t)
(format stream "~s ~s" (name object) (identity object))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; VARIABLE-TYPE.
;;; This class is used to augment an environment with the type of a
;;; variable, introduced by a declaration in a special form that
;;; allows such declarations.
(defclass variable-type (entry)
((%name :initarg :name :reader name)
(%type :initarg :type :reader type)))
(defmethod add-variable-type (environment symbol type)
(make-instance 'variable-type
:next environment
:name symbol
:type type))
(defmethod print-object ((object variable-type) stream)
(print-unreadable-object (object stream :type t :identity t)
(format stream "~s ~s" (name object) (type object))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; FUNCTION-TYPE.
;;; This class is used to augment an environment with the type of a
;;; function, introduced by a declaration in a special form that
;;; allows such declarations.
(defclass function-type (entry)
((%name :initarg :name :reader name)
(%type :initarg :type :reader type)))
(defmethod add-function-type (environment function-name type)
(make-instance 'function-type
:next environment
:name function-name
:type type))
(defmethod print-object ((object function-type) stream)
(print-unreadable-object (object stream :type t :identity t)
(format stream "~s ~s" (name object) (type object))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; VARIABLE-IGNORE.
;;; This class is used to augment an environment with an IGNORE
;;; declarations of a variable, introduced by a declaration in a
;;; special form that allows such declarations.
(defclass variable-ignore (entry)
((%name :initarg :name :reader name)
(%ignore :initarg :ignore :reader ignore)))
(defmethod add-variable-ignore (environment symbol ignore)
(make-instance 'variable-ignore
:next environment
:name symbol
:ignore ignore))
(defmethod print-object ((object variable-ignore) stream)
(print-unreadable-object (object stream :type t :identity t)
(format stream "~s ~s" (name object) (ignore object))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; FUNCTION-IGNORE.
;;; This class is used to augment an environment with an IGNORE
;;; declarations of a function, introduced by a declaration in a
;;; special form that allows such declarations.
(defclass function-ignore (entry)
((%name :initarg :name :reader name)
(%ignore :initarg :ignore :reader ignore)))
(defmethod add-function-ignore (environment function-name ignore)
(make-instance 'function-ignore
:next environment
:name function-name
:ignore ignore))
(defmethod print-object ((object function-ignore) stream)
(print-unreadable-object (object stream :type t :identity t)
(format stream "~s ~s" (name object) (ignore object))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; VARIABLE-DYNAMIC-EXTENT.
;;; This class is used to augment an environment with a DYNAMIC-EXTENT
;;; declarations of a variable, introduced by a declaration in a
;;; special form that allows such declarations.
(defclass variable-dynamic-extent (entry)
((%name :initarg :name :reader name)))
(defmethod add-variable-dynamic-extent (environment symbol)
(make-instance 'variable-dynamic-extent
:next environment
:name symbol))
(defmethod print-object ((object variable-dynamic-extent) stream)
(print-unreadable-object (object stream :type t :identity t)
(format stream "~s" (name object))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; FUNCTION-DYNAMIC-EXTENT.
;;; This class is used to augment an environment with a DYNAMIC-EXTENT
;;; declarations of a function, introduced by a declaration in a
;;; special form that allows such declarations.
(defclass function-dynamic-extent (entry)
((%name :initarg :name :reader name)))
(defmethod add-function-dynamic-extent (environment function-name)
(make-instance 'function-dynamic-extent
:next environment
:name function-name))
(defmethod print-object ((object function-dynamic-extent) stream)
(print-unreadable-object (object stream :type t :identity t)
(format stream "~s" (name object))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; OPTIMIZE.
;;; This class is used to augment an environment with an OPTIMIZE
;;; declarations, introduced by a declaration in a special form that
;;; allows such declarations.
(defclass optimize (entry)
((%optimize :initarg :optimize :reader optimize)
(%policy :initarg :policy :reader policy)))
(defmethod add-optimize (environment optimize policy)
(make-instance 'optimize
:next environment
:optimize optimize
:policy policy))
(defmethod print-object ((object optimize) stream)
(print-unreadable-object (object stream :type t :identity t)
(format stream "~s" (optimize object))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; INLINE.
;;; This class is used to augment an environment with an INLINE
;;; declarations of a function, introduced by a declaration in a
;;; special form that allows such declarations.
(defclass inline (entry)
((%name :initarg :name :reader name)
(%inline :initarg :inline :reader inline)))
(defmethod add-inline (environment function-name inline)
(make-instance 'inline
:next environment
:name function-name
:inline inline))
(defmethod print-object ((object inline) stream)
(print-unreadable-object (object stream :type t :identity t)
(format stream "~s ~s" (name object) (inline object))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;;
;;; INLINE-EXPANSION.
;;; This class is used to augment an environment with an inline
;;; expansion for a function (i.e., the :ast initarg to
;;; local- and global-function-info).
;;; It is separate from INLINE because having an expansion doesn't
;;; mean using it, and separate from the defining info because
;;; sometimes you have the name but want to add the expansion lower
;;; down.
(defclass inline-expansion (entry)
((%name :initarg :name :reader name)
(%ast :initarg :ast :reader ast)))
(defmethod add-inline-expansion (environment function-name expansioN)
(make-instance 'inline-expansion
:next environment
:name function-name
:ast expansion))
(defmethod print-object ((object inline-expansion) stream)
(print-unreadable-object (object stream :type t :identity t)
(format stream "~s" (name object))))