3 ;;;; Copyright (C) 2000,2001,2002, 2006, 2009, 2010, 2013, 2015 Free Software Foundation, Inc.
5 ;;;; This library is free software; you can redistribute it and/or
6 ;;;; modify it under the terms of the GNU Lesser General Public
7 ;;;; License as published by the Free Software Foundation; either
8 ;;;; version 3 of the License, or (at your option) any later version.
10 ;;;; This library is distributed in the hope that it will be useful,
11 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ;;;; Lesser General Public License for more details.
15 ;;;; You should have received a copy of the GNU Lesser General Public
16 ;;;; License along with this library; if not, write to the Free Software
17 ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 (define-module (oop goops save)
22 :use-module (oop goops internal)
23 :export (make-unbound save-objects load-objects restore
24 enumerate! enumerate-component!
25 write-readably write-component write-component-procedure
26 literal? readable make-readable))
28 (define (make-unbound)
32 ;;; save-objects ALIST PORT [EXCLUDED] [USES]
34 ;;; ALIST ::= ((NAME . OBJECT) ...)
36 ;;; Save OBJECT ... to PORT so that when the data is read and evaluated
37 ;;; OBJECT ... are re-created under names NAME ... .
38 ;;; Exclude any references to objects in the list EXCLUDED.
39 ;;; Add a (use-modules . USES) line to the top of the saved text.
41 ;;; In some instances, when `save-object' doesn't know how to produce
42 ;;; readable syntax for an object, you can explicitly register read
43 ;;; syntax for an object using the special form `readable'.
47 ;;; The function `foo' produces an object of obscure structure.
48 ;;; Only `foo' can construct such objects. Because of this, an
51 ;;; (define x (vector 1 (foo)))
53 ;;; cannot be saved by `save-objects'. But if you instead write
55 ;;; (define x (vector 1 (readable (foo))))
57 ;;; `save-objects' will happily produce the necessary read syntax.
59 ;;; To add new read syntax, hang methods on `enumerate!' and
62 ;;; enumerate! OBJECT ENV
63 ;;; Should call `enumerate-component!' (which takes same args) on
64 ;;; each component object. Should return #t if the composite object
65 ;;; can be written as a literal. (`enumerate-component!' returns #t
66 ;;; if the component is a literal.
68 ;;; write-readably OBJECT PORT ENV
69 ;;; Should write a readable representation of OBJECT to PORT.
70 ;;; Should use `write-component' to print each component object.
71 ;;; Use `literal?' to decide if a component is a literal.
75 ;;; enumerate-component! OBJECT ENV
77 ;;; write-component OBJECT PATCHER PORT ENV
78 ;;; PATCHER is an expression which, when evaluated, stores OBJECT
79 ;;; into its current location.
83 ;;; (write-component (car ls) `(set-car! ,ls ,(car ls)) file env)
85 ;;; write-component is a macro.
87 ;;; literal? COMPONENT ENV
90 (define-method (immediate? (o <top>)) #f)
92 (define-method (immediate? (o <null>)) #t)
93 (define-method (immediate? (o <number>)) #t)
94 (define-method (immediate? (o <boolean>)) #t)
95 (define-method (immediate? (o <symbol>)) #t)
96 (define-method (immediate? (o <char>)) #t)
97 (define-method (immediate? (o <keyword>)) #t)
99 ;;; enumerate! OBJECT ENVIRONMENT
101 ;;; Return #t if object is a literal.
103 (define-method (enumerate! (o <top>) env) #t)
105 (define-method (write-readably (o <top>) file env)
106 ;;(goops-error "No read-syntax defined for object `~S'" o)
107 (write o file) ;doesn't catch bugs, but is much more flexible
114 (define readables (make-weak-key-hash-table 61))
116 (define-macro (readable exp)
117 `(make-readable ,exp ',(copy-tree exp)))
119 (define (make-readable obj expr)
120 (hashq-set! readables obj expr)
123 (define (readable-expression obj)
124 `(readable ,(hashq-ref readables obj)))
126 ;; FIXME: if obj is nil or false, this can return a false value. OTOH
127 ;; usually this is only for non-immediates.
128 (define (readable? obj)
129 (hashq-ref readables obj))
135 (define (write-component-procedure o file env)
136 "Return #f if circular reference"
137 (cond ((immediate? o) (write o file) #t)
138 ((readable? o) (write (readable-expression o) file) #t)
139 ((excluded? o env) (display #f file) #t)
141 (let ((info (object-info o env)))
142 (cond ((not (binding? info)) (write-readably o file env) #t)
143 ((not (eq? (visiting info) #:defined)) #f) ;forward reference
144 (else (display (binding info) file) #t))))))
146 ;;; write-component OBJECT PATCHER FILE ENV
148 (define-macro (write-component object patcher file env)
149 `(or (write-component-procedure ,object ,file ,env)
152 (add-patcher! ,patcher ,env))))
158 (define-method (enumerate! (o <string>) env) #f)
164 (define-method (enumerate! (o <vector>) env)
165 (or (not (vector? o))
167 (array-for-each (lambda (o)
168 (if (not (enumerate-component! o env))
173 (define-method (write-readably (o <vector>) file env)
174 (if (not (vector? o))
176 (let ((n (vector-length o)))
179 (let ((not-literal? (not (literal? o env))))
180 (display (if not-literal?
184 (if (and not-literal?
185 (literal? (vector-ref o 0) env))
187 (write-component (vector-ref o 0)
188 `(vector-set! ,o 0 ,(vector-ref o 0))
193 (display #\space file)
194 (if (and not-literal?
195 (literal? (vector-ref o i) env))
197 (write-component (vector-ref o i)
198 `(vector-set! ,o ,i ,(vector-ref o i))
201 (display #\) file))))))
208 (define-method (enumerate! (o <array>) env)
209 (enumerate-component! (shared-array-root o) env))
211 (define (make-mapper array)
212 (let* ((n (array-rank array))
213 (indices (reverse (if (<= n 11)
214 (list-tail '(t s r q p n m l k j i) (- 11 n))
220 (cons (gensym "i") ls))))))))
222 (+ ,(shared-array-offset array)
223 ,@(map (lambda (ind dim inc)
224 `(* ,inc ,(if (pair? dim) `(- ,ind ,(car dim)) ind)))
226 (array-dimensions array)
227 (shared-array-increments array))))))
229 (define (write-array prefix o not-literal? file env)
230 (letrec ((inner (lambda (n indices)
232 (let ((el (apply array-ref o
233 (reverse (cons 0 indices)))))
234 (if (and not-literal?
239 `(array-set! ,o ,el ,@indices)
244 (display #\space file)
245 (let ((el (apply array-ref o
246 (reverse (cons i indices)))))
247 (if (and not-literal?
252 `(array-set! ,o ,el ,@indices)
255 (display prefix file)
256 (let loop ((dims (array-dimensions o))
258 (cond ((null? (cdr dims))
259 (inner (car dims) indices))
261 (let ((n (car dims)))
265 (display #\space file))
266 (display prefix file)
267 (loop (cdr dims) (cons i indices))
268 (display #\) file))))))
271 (define-method (write-readably (o <array>) file env)
272 (let ((root (shared-array-root o)))
273 (cond ((literal? o env)
274 (if (not (vector? root))
278 (display (array-rank o) file)
279 (write-array #\( o #f file env))))
281 (display "(make-shared-array " file)
282 (if (literal? root env)
284 (write-component root
285 (goops-error "write-readably(<array>): internal error")
288 (display #\space file)
289 (display (make-mapper o) file)
290 (for-each (lambda (dim)
291 (display #\space file)
293 (array-dimensions o))
296 (display "(list->uniform-array " file)
297 (display (array-rank o) file)
298 (display " '() " file)
299 (write-array "(list " o #f file env)))))
305 ;;; These methods have more complex structure than is required for
306 ;;; most objects, since they take over some of the logic of
307 ;;; `write-component'.
310 (define-method (enumerate! (o <pair>) env)
311 (let ((literal? (enumerate-component! (car o) env)))
312 (and (enumerate-component! (cdr o) env)
315 (define-method (write-readably (o <pair>) file env)
316 (let ((proper? (let loop ((ls o))
319 (not (binding? (cdr ls) env))
321 (1? (or (not (pair? (cdr o)))
322 (binding? (cdr o) env)))
323 (not-literal? (not (literal? o env)))
325 (refs (ref-stack env)))
326 (display (cond ((not not-literal?) #\()
331 (if (and not-literal?
332 (literal? (car o) env))
334 (write-component (car o) `(set-car! ,o ,(car o)) file env)
335 (do ((ls (cdr o) (cdr ls))
337 ((or (not (pair? ls))
341 (if (not not-literal?)
343 (display #\space file)
344 (if (and not-literal?
347 (write-component ls `(set-cdr! ,prev ,ls) file env)))
349 (display #\space file)
350 (set! infos (cons (object-info ls env) infos))
351 (push-ref! ls env) ;*fixme* optimize
352 (set! (visiting? (car infos)) #t)
353 (if (and not-literal?
354 (literal? (car ls) env))
356 (write-component (car ls) `(set-car! ,ls ,(car ls)) file env)
358 (for-each (lambda (info)
359 (set! (visiting? info) #f))
361 (set! (ref-stack env) refs)
368 ;;; Doesn't yet handle unbound slots
370 ;; Don't export this function! This is all very temporary.
372 (define (get-set-for-each proc class)
373 (for-each (lambda (slot)
374 (unless (memq (slot-definition-allocation slot)
375 '(#:class #:each-subclass))
376 (let ((ref (slot-definition-slot-ref slot))
377 (set (slot-definition-slot-set! slot))
378 (index (slot-definition-index slot)))
381 (proc (standard-get index) (standard-set index))))))
382 (class-slots class)))
384 (define (access-for-each proc class)
385 (for-each (lambda (slot)
386 (unless (memq (slot-definition-allocation slot)
387 '(#:class #:each-subclass))
388 (let ((name (slot-definition-name slot))
389 (accessor (and=> (slot-definition-accessor slot)
390 generic-function-name))
391 (ref (slot-definition-slot-ref slot))
392 (set (slot-definition-slot-set! slot))
393 (index (slot-definition-index slot)))
395 (proc name accessor ref set)
397 (standard-get index) (standard-set index))))))
398 (class-slots class)))
400 (define-macro (restore class slots . exps)
401 "(restore CLASS (SLOT-NAME1 ...) EXP1 ...)"
402 `(let ((o ((@@ (oop goops) %allocate-instance) ,class '())))
403 (for-each (lambda (name val)
404 (slot-set! o name val))
409 (define-method (enumerate! (o <object>) env)
410 (get-set-for-each (lambda (get set)
412 (if (not (unbound? val))
413 (enumerate-component! val env))))
417 (define-method (write-readably (o <object>) file env)
418 (let ((class (class-of o)))
419 (display "(restore " file)
420 (display (class-name class) file)
423 (filter (lambda (slotdef)
424 (not (or (memq (slot-definition-allocation slotdef)
425 '(#:class #:each-subclass))
426 (and (slot-bound? o (slot-definition-name slotdef))
428 (slot-ref o (slot-definition-name slotdef))
430 (class-slots class))))
431 (if (not (null? slotdefs))
433 (display (slot-definition-name (car slotdefs)) file)
434 (for-each (lambda (slotdef)
435 (display #\space file)
436 (display (slot-definition-name slotdef) file))
439 (access-for-each (lambda (name aname get set)
440 (display #\space file)
442 (cond ((unbound? val)
443 (display '(make-unbound) file))
444 ((excluded? val env))
446 (if (literal? val env)
450 `(set! (,aname ,o) ,val)
451 `(slot-set! ,o ',name ,val))
460 ;;; Currently, we don't support reading in class objects
463 (define-method (enumerate! (o <class>) env) #f)
465 (define-method (write-readably (o <class>) file env)
466 (display (class-name o) file))
472 ;;; Currently, we don't support reading in generic functions
475 (define-method (enumerate! (o <generic>) env) #f)
477 (define-method (write-readably (o <generic>) file env)
478 (display (generic-function-name o) file))
484 ;;; Currently, we don't support reading in methods
487 (define-method (enumerate! (o <method>) env) #f)
489 (define-method (write-readably (o <method>) file env)
490 (goops-error "No read-syntax for <method> defined"))
496 (define-class <environment> ()
497 (object-info #:accessor object-info
498 #:init-form (make-hash-table 61))
499 (excluded #:accessor excluded
500 #:init-form (make-hash-table 61))
501 (pass-2? #:accessor pass-2?
503 (ref-stack #:accessor ref-stack
505 (objects #:accessor objects
507 (pre-defines #:accessor pre-defines
509 (locals #:accessor locals
511 (stand-ins #:accessor stand-ins
513 (post-defines #:accessor post-defines
515 (patchers #:accessor patchers
517 (multiple-bound #:accessor multiple-bound
521 (define-method (initialize (env <environment>) initargs)
523 (cond ((get-keyword #:excluded initargs #f)
524 => (lambda (excludees)
525 (for-each (lambda (e)
526 (hashq-create-handle! (excluded env) e #f))
529 (define-method (object-info o env)
530 (hashq-ref (object-info env) o))
532 (define-method ((setter object-info) o env x)
533 (hashq-set! (object-info env) o x))
535 (define (excluded? o env)
536 (hashq-get-handle (excluded env) o))
538 (define (add-patcher! patcher env)
539 (set! (patchers env) (cons patcher (patchers env))))
541 (define (push-ref! o env)
542 (set! (ref-stack env) (cons o (ref-stack env))))
544 (define (pop-ref! env)
545 (set! (ref-stack env) (cdr (ref-stack env))))
547 (define (container env)
548 (car (ref-stack env)))
550 (define-class <object-info> ()
551 (visiting #:accessor visiting
553 (binding #:accessor binding
555 (literal? #:accessor literal?
559 (define visiting? visiting)
561 (define-method (binding (info <boolean>))
564 (define-method (binding o env)
565 (binding (object-info o env)))
567 (define binding? binding)
569 (define-method (literal? (info <boolean>))
572 ;;; Note that this method is intended to be used only during the
575 (define-method (literal? o env)
578 (let ((info (object-info o env)))
579 ;; write-component sets all bindings first to #:defining,
581 (and (or (not (binding? info))
582 ;; we might be using `literal?' in a write-readably method
583 ;; to query about the object being defined
584 (and (eq? (visiting info) #:defining)
585 (null? (cdr (ref-stack env)))))
592 ;;; Enumeration has two passes.
594 ;;; Pass 1: Detect common substructure, circular references and order
596 ;;; Pass 2: Detect literals
598 (define (enumerate-component! o env)
599 (cond ((immediate? o) #t)
601 ((excluded? o env) #t)
603 (let ((info (object-info o env)))
605 ;; if circular reference, we print as a literal
606 ;; (note that during pass-2, circular references are
607 ;; forward references, i.e. *not* yet marked with #:pass-2
608 (not (eq? (visiting? info) #:pass-2))
609 (and (enumerate! o env)
611 (set! (literal? info) #t)
615 (set! (binding info) #t)
617 ;; circular reference--mark container
618 (set! (binding (object-info (container env) env)) #t))))
620 (let ((info (make <object-info>)))
621 (set! (object-info o env) info)
623 (set! (visiting? info) #t)
625 (set! (visiting? info) #f)
627 (set! (objects env) (cons o (objects env)))))))
634 (define binding-name car)
635 (define binding-object cdr)
637 (define (pass-1! alist env)
638 ;; Determine object order and necessary bindings
639 (for-each (lambda (binding)
640 (enumerate-component! (binding-object binding) env))
643 (define (make-local i)
644 (string->symbol (string-append "%o" (number->string i))))
646 (define (name-bindings! alist env)
647 ;; Name top-level bindings
648 (for-each (lambda (b)
649 (let ((o (binding-object b)))
650 (if (not (or (immediate? o)
653 (let ((info (object-info o env)))
654 (if (symbol? (binding info))
655 ;; already bound to a variable
656 (set! (multiple-bound env)
657 (acons (binding info)
659 (multiple-bound env)))
661 (binding-name b)))))))
663 ;; Name rest of bindings and create stand-in and definition lists
664 (let post-loop ((ls (objects env))
666 (cond ((or (null? ls)
667 (eq? (binding (car ls) env) #t))
668 (set! (post-defines env) post-defs)
669 (set! (objects env) ls))
670 ((not (binding (car ls) env))
671 (post-loop (cdr ls) post-defs))
673 (post-loop (cdr ls) (cons (car ls) post-defs)))))
674 (let pre-loop ((ls (reverse (objects env)))
681 (set! (pre-defines env) (reverse pre-defs))
682 (set! (locals env) (reverse locs))
683 (set! (stand-ins env) (reverse sins)))
684 (let ((info (object-info (car ls) env)))
685 (cond ((not (binding? info))
686 (pre-loop (cdr ls) i pre-defs locs sins))
687 ((boolean? (binding info))
689 (set! (binding info) (make-local i))
698 (cons (car ls) pre-defs)
702 (let ((real-name (binding info)))
703 (set! (binding info) (make-local i))
708 (acons (binding info) real-name sins)))))))))
710 (define (pass-2! env)
711 (set! (pass-2? env) #t)
712 (for-each (lambda (o)
713 (let ((info (object-info o env)))
714 (set! (literal? info) (enumerate! o env))
715 (set! (visiting info) #:pass-2)))
716 (append (pre-defines env)
718 (post-defines env))))
720 (define (write-define! name val literal? file)
721 (display "(define " file)
723 (display #\space file)
724 (if literal? (display #\' file))
726 (display ")\n" file))
728 (define (write-empty-defines! file env)
729 (for-each (lambda (stand-in)
730 (write-define! (cdr stand-in) #f #f file))
732 (for-each (lambda (o)
733 (write-define! (binding o env) #f #f file))
736 (define (write-definition! prefix o file env)
737 (display prefix file)
738 (let ((info (object-info o env)))
739 (display (binding info) file)
740 (display #\space file)
744 (set! (visiting info) #:defining)
745 (write-readably o file env)
746 (set! (visiting info) #:defined)
750 (define (write-let*-head! file env)
751 (display "(let* (" file)
752 (write-definition! "(" (car (locals env)) file env)
753 (for-each (lambda (o)
754 (write-definition! "\n (" o file env))
756 (display ")\n" file))
758 (define (write-rebindings! prefix bindings file env)
759 (for-each (lambda (patch)
760 (display prefix file)
761 (display (cdr patch) file)
762 (display #\space file)
763 (display (car patch) file)
764 (display ")\n" file))
767 (define (write-definitions! selector prefix file env)
768 (for-each (lambda (o)
769 (write-definition! prefix o file env)
773 (define (write-patches! prefix file env)
774 (for-each (lambda (patch)
775 (display prefix file)
776 (display (let name-objects ((patcher patch))
777 (cond ((binding patcher env)
779 (cond ((assq name (stand-ins env))
783 (cons (name-objects (car patcher))
784 (name-objects (cdr patcher))))
788 (reverse (patchers env))))
790 (define (write-immediates! alist file)
791 (for-each (lambda (b)
792 (if (immediate? (binding-object b))
793 (write-define! (binding-name b)
799 (define (write-readables! alist file env)
801 (for-each (lambda (b)
802 (cond ((not (readable? (binding-object b))))
803 ((assq (binding-object b) written)
805 (set! (multiple-bound env)
808 (multiple-bound env)))))
810 (write-define! (binding-name b)
811 (readable-expression (binding-object b))
814 (set! written (acons (binding-object b)
819 (define-method (save-objects (alist <pair>) (file <string>) . rest)
820 (let ((port (open-output-file file)))
821 (apply save-objects alist port rest)
825 (define-method (save-objects (alist <pair>) (file <output-port>) . rest)
826 (let ((excluded (if (>= (length rest) 1) (car rest) '()))
827 (uses (if (>= (length rest) 2) (cadr rest) '())))
828 (let ((env (make <environment> #:excluded excluded)))
830 (name-bindings! alist env)
832 (if (not (null? uses))
834 (write `(use-modules ,@uses) file)
836 (write-immediates! alist file)
837 (if (null? (locals env))
839 (write-definitions! post-defines "(define " file env)
840 (write-patches! "" file env))
842 (write-definitions! pre-defines "(define " file env)
843 (write-empty-defines! file env)
844 (write-let*-head! file env)
845 (write-rebindings! " (set! " (stand-ins env) file env)
846 (write-definitions! post-defines " (set! " file env)
847 (write-patches! " " file env)
848 (display " )\n" file)))
849 (write-readables! alist file env)
850 (write-rebindings! "(define " (reverse (multiple-bound env)) file env))))
852 (define-method (load-objects (file <string>))
853 (let* ((port (open-input-file file))
854 (objects (load-objects port)))
858 (define iface (module-public-interface (current-module)))
860 (define-method (load-objects (file <input-port>))
861 (let ((m (make-module)))
862 (module-use! m the-scm-module)
863 (module-use! m iface)
864 (save-module-excursion
866 (set-current-module m)
867 (let loop ((sexp (read file)))
868 (if (not (eof-object? sexp))
871 (loop (read file)))))))
872 (module-map (lambda (name var)
873 (cons name (variable-ref var)))