-
Notifications
You must be signed in to change notification settings - Fork 4
/
general-purpose-asts.lisp
796 lines (626 loc) · 27.6 KB
/
general-purpose-asts.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
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
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
(cl:in-package #:cleavir-ast)
(defgeneric map-children (function ast)
(:argument-precedence-order ast function)
(:method-combination progn)
(:documentation "Call FUNCTION on all children of AST."))
(defgeneric children (ast)
(:method-combination append)
(:documentation "Return a list of children of an AST. Children are ASTs corresponding to subforms of the AST."))
(defmacro define-children (ast-class children-spec)
(multiple-value-bind (children rest-child)
(if (symbolp children-spec)
(values nil children-spec)
(let* ((last (last children-spec))
(last-cdr (cdr last)))
(if last-cdr
(values (append (butlast children-spec) (list (car last))) last-cdr)
(values children-spec nil))))
`(progn
(defmethod children append ((ast ,ast-class))
,(let ((access (loop for child in children
collect `(,child ast))))
(if rest-child
`(list* ,@access (,rest-child ast))
`(list ,@access))))
(defmethod map-children progn (function (ast ,ast-class))
,@(when (and (null children) (not rest-child))
`((declare (cl:ignore function))))
,@(loop for child in children
collect `(funcall function (,child ast)))
,(when rest-child
`(mapc function (,rest-child ast)))))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Variable *POLICY*. Default for :policy initarg.
;;; This is useful because every AST has a policy, but they're
;;; shared very heavily and generated all over the place.
(defvar *policy*)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Class AST.
;;;
;;; ORIGIN is a client-supplied object that is not interpreted by
;;; Cleavir.
;;; POLICY is the compilation policy in force for the AST.
(defclass ast ()
((%origin :initform nil :initarg :origin :accessor origin)
(%policy :initform *policy* :initarg :policy :accessor policy))
(:documentation "The base class for all abstract syntax tree classes."))
;;; Policies must be saved
(cleavir-io:define-save-info ast
(:origin origin)
(:policy policy))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; AST classes for standard common lisp features.
;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Class CONSTANT-AST.
;;;
(defclass constant-ast (ast)
((%value :initarg :value :reader value))
(:documentation "This class represents Lisp constants in source code.
If the constant that was found was wrapped in QUOTE, then the QUOTE is not part of the value here, because it was stripped off.
If the constant that was found was a constant variable, then the value here represents the value of that constant variable at compile time."))
(defun make-constant-ast (value &key origin (policy *policy*))
(make-instance 'constant-ast
:origin origin :policy policy
:value value))
(cleavir-io:define-save-info constant-ast
(:value value))
(define-children constant-ast ())
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Class LEXICAL-VARIABLE.
(defclass lexical-variable (ast)
(;; Only used for debugging purposes.
(%name :initarg :name :reader name))
(:documentation "Representation of a lexical variable."))
(defun make-lexical-variable (name &key origin (policy *policy*))
(make-instance 'lexical-variable
:origin origin :policy policy
:name name))
(cleavir-io:define-save-info lexical-variable
(:name name))
(define-children lexical-variable ())
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Class LEXICAL-AST.
;;;
(defclass lexical-ast (ast)
((%lexical-variable :initarg :lexical-variable :reader lexical-variable))
(:documentation "A reference to a lexical variable."))
(defun make-lexical-ast (lexical-variable &key origin (policy *policy*))
(make-instance 'lexical-ast
:origin origin :policy policy
:lexical-variable lexical-variable))
(cleavir-io:define-save-info lexical-ast
(:lexical-variable lexical-variable))
(define-children lexical-ast ())
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Class CONSTANT-SYMBOL-VALUE-AST.
(defclass constant-symbol-value-ast (ast)
((%name :initarg :name :reader name)))
(defun make-constant-symbol-value-ast (name &key origin (policy *policy*))
(make-instance 'constant-symbol-value-ast
:origin origin :policy policy
:name name))
(cleavir-io:define-save-info constant-symbol-value-ast
(:name name))
(define-children constant-symbol-value-ast ())
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Class SET-CONSTANT-SYMBOL-VALUE-AST.
(defclass set-constant-symbol-value-ast (ast)
((%name :initarg :name :reader name)
(%value-ast :initarg :value-ast :reader value-ast)))
(defun make-set-constant-symbol-value-ast (name value-ast &key origin (policy *policy*))
(make-instance 'set-constant-symbol-value-ast
:origin origin :policy policy
:name name
:value-ast value-ast))
(cleavir-io:define-save-info set-constant-symbol-value-ast
(:name name)
(:value-ast value-ast))
(define-children set-constant-symbol-value-ast (value-ast))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Class CONSTANT-FDEFINITION-AST.
;;;
(defclass constant-fdefinition-ast (ast)
(;; This slot contains the name of the function
(%name :initarg :name :reader name)
(%attributes :initarg :attributes :reader attributes
:initform (cleavir-attributes:default-attributes))))
(defun make-constant-fdefinition-ast (name
&key origin (policy *policy*)
(attributes
(cleavir-attributes:default-attributes)))
(make-instance 'constant-fdefinition-ast
:origin origin :policy policy
:name name :attributes attributes))
(cleavir-io:define-save-info constant-fdefinition-ast
(:name name)
(:attributes attributes))
(define-children constant-fdefinition-ast ())
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Class CALL-AST.
;;;
(defclass call-ast (ast)
((%callee-ast :initarg :callee-ast :reader callee-ast)
(%argument-asts :initarg :argument-asts :reader argument-asts)
(%inline :initarg :inline :initform nil :reader inline-declaration))
(:documentation "A function call."))
(defun make-call-ast (callee-ast argument-asts
&key origin inline (policy *policy*))
(make-instance 'call-ast
:origin origin :policy policy
:callee-ast callee-ast
:inline inline
:argument-asts argument-asts))
(cleavir-io:define-save-info call-ast
(:callee-ast callee-ast)
(:argument-asts argument-asts)
(:inline inline-declaration))
(define-children call-ast (callee-ast . argument-asts))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Class FUNCTION-AST.
;;;
(defclass function-ast (ast)
((%lambda-list :initarg :lambda-list :reader lambda-list)
(%body-ast :initarg :body-ast :reader body-ast)
;; An alist from lexical VARIABLEs to lists of pertinent declaration specifiers.
;; Since SPECIAL is otherwise handled, these are for optimization use only
;; and may be discarded at will.
(%bound-declarations :initarg :bound-declarations :initform nil
:reader bound-declarations)
;; These three are intended for debugging/introspection.
(%name :initarg :name :initform nil :accessor name)
(%docstring :initarg :docstring :initform nil :reader docstring)
(%original-lambda-list :initarg :original-lambda-list :initform nil
:accessor original-lambda-list)
(%attributes :initarg :attributes :reader attributes
:initform (cleavir-attributes:default-attributes)))
(:documentation "A function AST represents an explicit lambda expression, but also implicit lambda expressions such as the ones found in FLET and LABELS.
The lambda list is not a normal lambda list. It has the following form:
([r1 .. rl [&optional o1 ..om] [&rest r] [&key k1 .. kn &allow-other-keys]]])
where:
- Each ri is a LEXICAL-VARIABLE.
- r is a LEXICAL-VARIABLE.
- Each oi is a list of two LEXICAL-VARIABLEs. The second of the two conceptually contains a Boolean value indicating whether the first one contains a value supplied by the caller.
- Each ki is a list of a symbol and two LEXICAL-VARIABLEs. The symbol is the keyword-name that a caller must supply in order to pass the corresponding argument. The second of the two LEXICAL-VARIABLEs conceptually contains a Boolean value indicating whether the first LEXICAL-VARIABLE contains a value supplied by the caller.
The LEXICAL-VARIABLEs in the lambda list are potentially unrelated to the variables that were given in the original lambda expression, and they are LEXICAL-VARIABLEs independently of whether the corresponding variable that was given in the original lambda expression is a lexical variable or a special variable.
The body of the FUNCTION-AST must contain code that tests the second of the two LEXICAL-VARIABLEs and initializes variables if needed. The if the second LEXICAL-VARIABLE in any oi contains FALSE, then the code in the body is not allowed to test the second LEXICAL-VARIABLEs of any of the ki because they may not be set correctly (conceptually, they all have the value FALSE then)."))
(defun make-function-ast (body-ast lambda-list
&key name docstring original-lambda-list
bound-declarations
origin (policy *policy*)
(attributes
(cleavir-attributes:default-attributes)))
(make-instance 'function-ast
:origin origin :policy policy
:name name :docstring docstring
:original-lambda-list original-lambda-list
:bound-declarations bound-declarations
:body-ast body-ast
:lambda-list lambda-list
:attributes attributes))
(cleavir-io:define-save-info function-ast
(:lambda-list lambda-list)
(:body-ast body-ast)
(:name name) (:docstring docstring)
(:bound-declarations bound-declarations)
(:original-lambda-list original-lambda-list)
(:attributes attributes))
(defmethod children append ((ast function-ast))
(list* (body-ast ast)
(loop for entry in (lambda-list ast)
append (cond ((symbolp entry)
'())
((consp entry)
(if (= (length entry) 2)
entry
(cdr entry)))
(t
(list entry))))))
(defmethod map-children progn (function (ast function-ast))
(funcall function (body-ast ast))
(dolist (entry (lambda-list ast))
(cond ((symbolp entry))
((consp entry)
(if (= (length entry) 2)
(mapc function entry)
(mapc function (cdr entry))))
(t (funcall function entry)))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Class TOP-LEVEL-FUNCTION-AST.
;;;
(defclass top-level-function-ast (function-ast)
((%forms :initarg :forms :reader forms))
(:documentation "This AST is a subclass of FUNCTION-AST. It is used when an AST is transformed by hoisting all the LOAD-TIME-VALUE-ASTs in the tree by turning them into LEXIAL-ASTs that are also required parameters of the TOP-LEVEL-FUNCTION-AST.
This AST class supplies a slot that contains a list of the forms that were contained in the LOAD-TIME-VALUE-ASTs. In order to evaluate the original AST, the transformed AST must be called with the values of those forms as arguments."))
(defun make-top-level-function-ast (body-ast lambda-list forms
&key origin (policy *policy*))
(make-instance 'top-level-function-ast
:origin origin :policy policy
:body-ast body-ast
:lambda-list lambda-list
:forms forms))
(cleavir-io:define-save-info top-level-function-ast
(:forms forms))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Class INLINE-AST.
;;;
(defclass inline-ast (ast)
((%body :initarg :body-ast :reader body-ast))
(:documentation "An AST inserted around an inline function's AST, used to separate the inlined function from the rest of the code for AST-to-BIR."))
(cleavir-io:define-save-info inline-ast
(:body-ast body-ast))
(define-children inline-ast (body-ast))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Class PRIMOP-AST.
;;;
(defclass primop-ast (ast)
((%info :initarg :info :reader info)
(%argument-asts :initarg :argument-asts :reader argument-asts)
(%attributes :initarg :attributes :reader attributes
:initform (cleavir-attributes:default-attributes)))
(:documentation "A PRIMOP-AST represents the invocation of a primitive operator.
See the cleavir-primop system for more information."))
(cleavir-io:define-save-info primop-ast
(:info info)
(:argument-asts argument-asts)
(:attributes attributes))
(define-children primop-ast argument-asts)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Class PROGN-AST.
(defclass progn-ast (ast)
((%form-asts :initarg :form-asts :reader form-asts))
(:documentation "An AST representing a cl:progn."))
(defun make-progn-ast (form-asts &key origin (policy *policy*))
(make-instance 'progn-ast
:origin origin :policy policy
:form-asts form-asts))
(cleavir-io:define-save-info progn-ast
(:form-asts form-asts))
(define-children progn-ast form-asts)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Class BLOCK-AST.
(defclass block-ast (ast)
((%body-ast :initarg :body-ast :accessor body-ast)
;; Original name of the block, for debugging etc.
(%name :initarg :name :reader name))
(:documentation "AST representing a cl:block."))
(defun make-block-ast (body-ast &key name origin (policy *policy*))
(make-instance 'block-ast
:origin origin :policy policy :name name
:body-ast body-ast))
(cleavir-io:define-save-info block-ast
(:name name)
(:body-ast body-ast))
(define-children block-ast (body-ast))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Class RETURN-FROM-AST.
(defclass return-from-ast (ast)
((%block-ast :initarg :block-ast :reader block-ast)
(%form-ast :initarg :form-ast :reader form-ast))
(:documentation "AST representing cl:return-from.
This points directly to the corresponding BLOCK-AST rather than recording the name of the block. Note that the BLOCK-AST is not a child (subform)."))
(defun make-return-from-ast (block-ast form-ast &key origin (policy *policy*))
(make-instance 'return-from-ast
:origin origin :policy policy
:block-ast block-ast
:form-ast form-ast))
(cleavir-io:define-save-info return-from-ast
(:block-ast block-ast)
(:form-ast form-ast))
(define-children return-from-ast (form-ast))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Class SETQ-AST.
(defclass setq-ast (ast)
((%lexical-variable :initarg :lexical-variable :reader lexical-variable)
(%value-ast :initarg :value-ast :reader value-ast))
(:documentation "AST representing SETQ of a lexical variable."))
(defun make-setq-ast (lexical-variable value-ast &key origin (policy *policy*))
(make-instance 'setq-ast
:origin origin :policy policy
:lexical-variable lexical-variable
:value-ast value-ast))
(cleavir-io:define-save-info setq-ast
(:lexical-variable lexical-variable)
(:value-ast value-ast))
(define-children setq-ast (value-ast))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Class LEXICAL-BIND-AST.
;;;
(defclass lexical-bind-ast (ast)
((%lexical-variable :initarg :lexical-variable :reader lexical-variable)
(%value-ast :initarg :value-ast :reader value-ast)
(%ignore :initarg :ignore :reader ignore))
(:documentation "AST representing the binding of a lexical variable."))
(defun make-lexical-bind-ast (lexical-variable value-ast &key ignore origin (policy *policy*))
(make-instance 'lexical-bind-ast
:origin origin :policy policy
:lexical-variable lexical-variable
:value-ast value-ast
:ignore ignore))
(cleavir-io:define-save-info lexical-bind-ast
(:lexical-variable lexical-variable)
(:value-ast value-ast)
(:ignore ignore))
(define-children lexical-bind-ast (lexical-variable value-ast))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Class CONSTANT-DYNAMIC-BIND-AST
;;;
(defclass constant-dynamic-bind-ast (ast)
((%name :initarg :name :reader name)
(%value :initarg :value-ast :reader value-ast)
(%body :initarg :body-ast :reader body-ast))
(:documentation "This AST represents the binding of a special variable."))
(cleavir-io:define-save-info constant-dynamic-bind-ast
(:name name)
(:value-ast value-ast)
(:body-ast body-ast))
(define-children constant-dynamic-bind-ast
(value-ast body-ast))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Class TAG-AST.
(defclass tag-ast (ast)
((%name :initarg :name :reader name)
(%body-ast :initarg :body-ast :accessor body-ast))
(:documentation "The TAG-AST includes the tag itself, and also the code following it that is not after another tag."))
(defun make-tag-ast (name &key origin (policy *policy*))
(make-instance 'tag-ast
:origin origin :policy policy
:name name))
(cleavir-io:define-save-info tag-ast
(:name name) (:body-ast body-ast))
(define-children tag-ast (body-ast))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Class TAGBODY-AST.
(defclass tagbody-ast (ast)
((%prefix-ast :initarg :prefix-ast :reader prefix-ast)
;; A proper list of TAG-ASTs.
(%item-asts :initarg :item-asts :reader item-asts))
(:documentation "AST for a cl:tagbody."))
(defun make-tagbody-ast (prefix-ast item-asts &key origin (policy *policy*))
(make-instance 'tagbody-ast
:origin origin :policy policy
:prefix-ast prefix-ast :item-asts item-asts))
(cleavir-io:define-save-info tagbody-ast
(:prefix-ast prefix-ast) (:item-asts item-asts))
(define-children tagbody-ast (prefix-ast . item-asts))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Class GO-AST.
(defclass go-ast (ast)
((%tag-ast :initarg :tag-ast :reader tag-ast))
(:documentation "AST for a cl:go.
Includes the destination TAG-AST directly, rather than a name. Note that the TAG-AST is not a child (subform)."))
(defun make-go-ast (tag-ast &key origin (policy *policy*))
(make-instance 'go-ast
:origin origin :policy policy
:tag-ast tag-ast))
(cleavir-io:define-save-info go-ast
(:tag-ast tag-ast))
(define-children go-ast ())
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Class UNWIND-PROTECT-AST.
(defclass unwind-protect-ast (ast)
((%body :initarg :body-ast :reader body-ast)
(%cleanup :initarg :cleanup-ast :reader cleanup-ast))
(:documentation "AST for a cl:unwind-protect.
The cleanup is represented by a FUNCTION-AST with no arguments."))
(cleavir-io:define-save-info unwind-protect-ast
(:body-ast body-ast) (:cleanup-ast cleanup-ast))
(define-children unwind-protect-ast (body-ast cleanup-ast))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Class THE-AST.
;;;
(defclass the-ast (ast)
((%form-ast :initarg :form-ast :reader form-ast)
;; A VALUES ctype.
(%ctype :initarg :ctype :reader ctype)
;; This slot holds either a function which checks the FORM-AST,
;; :TRUSTED if we want to treat this as trusted type assertion with
;; no check needed, or NIL if the declaration is untrusted but not
;; necessarily checked. See BIR/instructions.lisp for the canonical
;; definition.
(%type-check-function-ast :initarg :type-check-function-ast :reader type-check-function-ast))
(:documentation "This AST can be generated by from the THE special operator, but also implicitly from type declarations and assignments to variables with type declarations."))
(defun make-the-ast (form-ast ctype type-check-function-ast &key origin (policy *policy*))
(make-instance 'the-ast
:origin origin :policy policy
:form-ast form-ast :ctype ctype
:type-check-function-ast type-check-function-ast))
(cleavir-io:define-save-info the-ast
(:form-ast form-ast)
(:ctype ctype)
(:type-check-function-ast type-check-function-ast))
(defmethod children append ((ast the-ast))
(let ((form-ast (form-ast ast))
(type-check-function-ast (type-check-function-ast ast)))
(if (symbolp type-check-function-ast)
(list form-ast)
(list form-ast type-check-function-ast))))
(defmethod map-children progn (function (ast the-ast))
(let ((form-ast (form-ast ast))
(type-check-function-ast (type-check-function-ast ast)))
(cond ((symbolp type-check-function-ast)
(funcall function form-ast))
(t
(funcall function form-ast)
(funcall function type-check-function-ast)))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Class TYPEQ-AST.
;;;
(defclass typeq-ast (ast)
((%test-ctype :initarg :test-ctype :reader test-ctype)
(%form-ast :initarg :form-ast :reader form-ast))
(:documentation "This AST can be thought of as a translation to an AST of a special form (TYPEQ <form> <type-specifier>) which is like the function TYPEP, except that the type specifier is not evaluated."))
(defun make-typeq-ast (form-ast test-ctype &key origin (policy *policy*))
(make-instance 'typeq-ast
:origin origin :policy policy
:form-ast form-ast
:test-ctype test-ctype))
(cleavir-io:define-save-info typeq-ast
(:test-ctype test-ctype)
(:form-ast form-ast))
(define-children typeq-ast (form-ast))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Class LOAD-TIME-VALUE-AST.
;;;
(defclass load-time-value-ast (ast)
((%form :initarg :form :reader form)
(%read-only-p :initarg :read-only-p :reader read-only-p))
(:documentation "This AST corresponds directly to the LOAD-TIME-VALUE special operator. It has no child and it produces a single value.
The optional argument READ-ONLY-P is not a child of the AST because it can only be a Boolean which is not evaluated, so we know at AST creation time whether it is true or false."))
(defun make-load-time-value-ast (form &optional read-only-p &key origin (policy *policy*))
(make-instance 'load-time-value-ast
:origin origin :policy policy
:form form
:read-only-p read-only-p))
;;; Even though READ-ONLY-P is not a child of the AST, it needs to be
;;; saved when the AST is saved.
(cleavir-io:define-save-info load-time-value-ast
(:form form)
(:read-only-p read-only-p))
(define-children load-time-value-ast ())
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Class IF-AST.
;;;
(defclass if-ast (ast)
((%test-ast :initarg :test-ast :reader test-ast)
(%then-ast :initarg :then-ast :reader then-ast)
(%else-ast :initarg :else-ast :reader else-ast))
(:documentation "This AST corresponds directly to the IF special operator."))
(defun make-if-ast (test-ast then-ast else-ast &key origin (policy *policy*))
(make-instance 'if-ast
:origin origin :policy policy
:test-ast test-ast
:then-ast then-ast
:else-ast else-ast))
(cleavir-io:define-save-info if-ast
(:test-ast test-ast)
(:then-ast then-ast)
(:else-ast else-ast))
(define-children if-ast (test-ast then-ast else-ast))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Class BRANCH-AST.
;;;
(defclass branch-ast (ast)
((%test-ast :initarg :test-ast :reader test-ast)
(%branch-asts :initarg :branch-asts :reader branch-asts)
(%default-ast :initarg :default-ast :reader default-ast))
(:documentation "This class is a generalization of IF-AST. Based on the TEST-AST, one of the zero or more BRANCH-ASTs, or else the DEFAULT-AST, will be evaluated and its values returned.
This AST can be used for example in the implementation of fast CASE or TYPECASE operations."))
(defun make-branch-ast (test-ast branch-asts default-ast
&key origin (policy *policy*))
(make-instance 'branch-ast
:origin origin :policy policy
:test-ast test-ast
:branch-asts branch-asts :default-ast default-ast))
(cleavir-io:define-save-info branch-ast
(:test-ast test-ast)
(:branch-asts branch-asts)
(:default-ast default-ast))
(define-children branch-ast (test-ast default-ast . branch-asts))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Class MULTIPLE-VALUE-CALL-AST.
(defclass multiple-value-call-ast (ast)
((%function-form-ast :initarg :function-form-ast :reader function-form-ast)
(%form-asts :initarg :form-asts :reader form-asts))
(:documentation "AST for cl:multiple-value-call. Unlike the special operator, only actual functions are acceptable, not all function designators."))
(defun make-multiple-value-call-ast
(function-form-ast form-asts &key origin (policy *policy*))
(make-instance 'multiple-value-call-ast
:origin origin :policy policy
:function-form-ast function-form-ast :form-asts form-asts))
(cleavir-io:define-save-info multiple-value-call-ast
(:function-form-ast function-form-ast)
(:form-asts form-asts))
(define-children multiple-value-call-ast (function-form-ast . form-asts))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Class MULTIPLE-VALUE-PROG1-AST.
(defclass multiple-value-prog1-ast (ast)
((%first-form-ast :initarg :first-form-ast :reader first-form-ast)
;; A list of ASTs
(%form-asts :initarg :form-asts :reader form-asts))
(:documentation "AST for cl:multiple-value-prog1"))
(defun make-multiple-value-prog1-ast (first-form-ast form-asts &key origin (policy *policy*))
(make-instance 'multiple-value-prog1-ast
:origin origin :policy policy
:first-form-ast first-form-ast
:form-asts form-asts))
(cleavir-io:define-save-info multiple-value-prog1-ast
(:first-form-ast first-form-ast)
(:form-asts form-asts))
(define-children multiple-value-prog1-ast (first-form-ast . form-asts))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Class UNREACHABLE-AST.
;;;
(defclass unreachable-ast (ast) ()
(:documentation "This AST indicates an unreachable control point. Control that leads inevitably from or to this AST is declared to be impossible.
This impossibility is used by the analyzer without checking."))
(defun make-unreachable-ast (&key origin (policy *policy*))
(make-instance 'unreachable-ast :origin origin :policy policy))
(define-children unreachable-ast ())
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Class EQ-AST.
;;;
(defclass eq-ast (ast)
((%arg1-ast :initarg :arg1-ast :reader arg1-ast)
(%arg2-ast :initarg :arg2-ast :reader arg2-ast))
(:documentation "This AST can be used to to test whether two objects are identical.
It has two children. This AST can only appear in the TEST position of an IF-AST."))
(defun make-eq-ast (arg1-ast arg2-ast &key origin (policy *policy*))
(make-instance 'eq-ast
:origin origin :policy policy
:arg1-ast arg1-ast
:arg2-ast arg2-ast))
(cleavir-io:define-save-info eq-ast
(:arg1-ast arg1-ast)
(:arg2-ast arg2-ast))
(define-children eq-ast (arg1-ast arg2-ast))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Class CASE-AST.
;;;
(defclass case-ast (ast)
((%arg-ast :initarg :arg-ast :reader arg-ast)
(%comparees :initarg :comparees :reader comparees))
(:documentation "This AST can be used to select an execution path by comparing a given object against a fixed set of immediates. COMPAREES is a sequence of sequences of objects. If the primary value returned by the ARG-AST is EQ to one of the objects in the nth sequence, the nth branch is taken; if the value doesn't match any immediate the default branch is taken instead.
This AST can only appear in the TEST position of a BRANCH-AST."))
(defun make-case-ast (arg-ast comparees &key origin (policy *policy*))
(make-instance 'case-ast
:origin origin :policy policy
:arg-ast arg-ast :comparees comparees))
(cleavir-io:define-save-info case-ast
(:arg-ast arg-ast)
(:comparees comparees))
(define-children case-ast (arg-ast))