3 ;;;; Copyright (C) 2000,2001,2002, 2006 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 2.1 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 :use-module (oop goops util)
24 :re-export (make-unbound)
25 :export (save-objects load-objects restore
26 enumerate! enumerate-component!
27 write-readably write-component write-component-procedure
28 literal? readable make-readable))
31 ;;; save-objects ALIST PORT [EXCLUDED] [USES]
33 ;;; ALIST ::= ((NAME . OBJECT) ...)
35 ;;; Save OBJECT ... to PORT so that when the data is read and evaluated
36 ;;; OBJECT ... are re-created under names NAME ... .
37 ;;; Exclude any references to objects in the list EXCLUDED.
38 ;;; Add a (use-modules . USES) line to the top of the saved text.
40 ;;; In some instances, when `save-object' doesn't know how to produce
41 ;;; readable syntax for an object, you can explicitly register read
42 ;;; syntax for an object using the special form `readable'.
46 ;;; The function `foo' produces an object of obscure structure.
47 ;;; Only `foo' can construct such objects. Because of this, an
50 ;;; (define x (vector 1 (foo)))
52 ;;; cannot be saved by `save-objects'. But if you instead write
54 ;;; (define x (vector 1 (readable (foo))))
56 ;;; `save-objects' will happily produce the necessary read syntax.
58 ;;; To add new read syntax, hang methods on `enumerate!' and
61 ;;; enumerate! OBJECT ENV
62 ;;; Should call `enumerate-component!' (which takes same args) on
63 ;;; each component object. Should return #t if the composite object
64 ;;; can be written as a literal. (`enumerate-component!' returns #t
65 ;;; if the component is a literal.
67 ;;; write-readably OBJECT PORT ENV
68 ;;; Should write a readable representation of OBJECT to PORT.
69 ;;; Should use `write-component' to print each component object.
70 ;;; Use `literal?' to decide if a component is a literal.
74 ;;; enumerate-component! OBJECT ENV
76 ;;; write-component OBJECT PATCHER PORT ENV
77 ;;; PATCHER is an expression which, when evaluated, stores OBJECT
78 ;;; into its current location.
82 ;;; (write-component (car ls) `(set-car! ,ls ,(car ls)) file env)
84 ;;; write-component is a macro.
86 ;;; literal? COMPONENT ENV
89 (define-method (immediate? (o <top>)) #f)
91 (define-method (immediate? (o <null>)) #t)
92 (define-method (immediate? (o <number>)) #t)
93 (define-method (immediate? (o <boolean>)) #t)
94 (define-method (immediate? (o <symbol>)) #t)
95 (define-method (immediate? (o <char>)) #t)
96 (define-method (immediate? (o <keyword>)) #t)
98 ;;; enumerate! OBJECT ENVIRONMENT
100 ;;; Return #t if object is a literal.
102 (define-method (enumerate! (o <top>) env) #t)
104 (define-method (write-readably (o <top>) file env)
105 ;;(goops-error "No read-syntax defined for object `~S'" o)
106 (write o file) ;doesn't catch bugs, but is much more flexible
113 (if (or (not (defined? 'readables))
115 (define readables (make-weak-key-hash-table 61)))
118 (procedure->memoizing-macro
120 `(make-readable ,(cadr exp) ',(copy-tree (cadr exp))))))
122 (define (make-readable obj expr)
123 (hashq-set! readables obj expr)
126 (define (readable-expression obj)
127 `(readable ,(hashq-ref readables obj)))
129 (define (readable? obj)
130 (hashq-get-handle readables obj))
136 (define-method (enumerate! (o <string>) env) #f)
142 (define-method (enumerate! (o <vector>) env)
143 (or (not (vector? o))
145 (array-for-each (lambda (o)
146 (if (not (enumerate-component! o env))
151 (define-method (write-readably (o <vector>) file env)
152 (if (not (vector? o))
154 (let ((n (vector-length o)))
157 (let ((not-literal? (not (literal? o env))))
158 (display (if not-literal?
162 (if (and not-literal?
163 (literal? (vector-ref o 0) env))
165 (write-component (vector-ref o 0)
166 `(vector-set! ,o 0 ,(vector-ref o 0))
171 (display #\space file)
172 (if (and not-literal?
173 (literal? (vector-ref o i) env))
175 (write-component (vector-ref o i)
176 `(vector-set! ,o ,i ,(vector-ref o i))
179 (display #\) file))))))
186 (define-method (enumerate! (o <array>) env)
187 (enumerate-component! (shared-array-root o) env))
189 (define (make-mapper array)
190 (let* ((dims (array-dimensions array))
191 (n (array-rank array))
192 (indices (reverse (if (<= n 11)
193 (list-tail '(t s r q p n m l k j i) (- 11 n))
199 (cons (gensym "i") ls))))))))
201 (+ ,(shared-array-offset array)
202 ,@(map (lambda (ind dim inc)
203 `(* ,inc ,(if (pair? dim) `(- ,ind ,(car dim)) ind)))
205 (array-dimensions array)
206 (shared-array-increments array))))))
208 (define (write-array prefix o not-literal? file env)
209 (letrec ((inner (lambda (n indices)
211 (let ((el (apply array-ref o
212 (reverse (cons 0 indices)))))
213 (if (and not-literal?
218 `(array-set! ,o ,el ,@indices)
223 (display #\space file)
224 (let ((el (apply array-ref o
225 (reverse (cons i indices)))))
226 (if (and not-literal?
231 `(array-set! ,o ,el ,@indices)
234 (display prefix file)
235 (let loop ((dims (array-dimensions o))
237 (cond ((null? (cdr dims))
238 (inner (car dims) indices))
240 (let ((n (car dims)))
244 (display #\space file))
245 (display prefix file)
246 (loop (cdr dims) (cons i indices))
247 (display #\) file))))))
250 (define-method (write-readably (o <array>) file env)
251 (let ((root (shared-array-root o)))
252 (cond ((literal? o env)
253 (if (not (vector? root))
257 (display (array-rank o) file)
258 (write-array #\( o #f file env))))
260 (display "(make-shared-array " file)
261 (if (literal? root env)
263 (write-component root
264 (goops-error "write-readably(<array>): internal error")
267 (display #\space file)
268 (display (make-mapper o) file)
269 (for-each (lambda (dim)
270 (display #\space file)
272 (array-dimensions o))
275 (display "(list->uniform-array " file)
276 (display (array-rank o) file)
277 (display " '() " file)
278 (write-array "(list " o file env)))))
284 ;;; These methods have more complex structure than is required for
285 ;;; most objects, since they take over some of the logic of
286 ;;; `write-component'.
289 (define-method (enumerate! (o <pair>) env)
290 (let ((literal? (enumerate-component! (car o) env)))
291 (and (enumerate-component! (cdr o) env)
294 (define-method (write-readably (o <pair>) file env)
295 (let ((proper? (let loop ((ls o))
298 (not (binding? (cdr ls) env))
300 (1? (or (not (pair? (cdr o)))
301 (binding? (cdr o) env)))
302 (not-literal? (not (literal? o env)))
304 (refs (ref-stack env)))
305 (display (cond ((not not-literal?) #\()
310 (if (and not-literal?
311 (literal? (car o) env))
313 (write-component (car o) `(set-car! ,o ,(car o)) file env)
314 (do ((ls (cdr o) (cdr ls))
316 ((or (not (pair? ls))
320 (if (not not-literal?)
322 (display #\space file)
323 (if (and not-literal?
326 (write-component ls `(set-cdr! ,prev ,ls) file env)))
328 (display #\space file)
329 (set! infos (cons (object-info ls env) infos))
330 (push-ref! ls env) ;*fixme* optimize
331 (set! (visiting? (car infos)) #t)
332 (if (and not-literal?
333 (literal? (car ls) env))
335 (write-component (car ls) `(set-car! ,ls ,(car ls)) file env)
337 (for-each (lambda (info)
338 (set! (visiting? info) #f))
340 (set! (ref-stack env) refs)
347 ;;; Doesn't yet handle unbound slots
349 ;; Don't export this function! This is all very temporary.
351 (define (get-set-for-each proc class)
352 (for-each (lambda (slotdef g-n-s)
353 (let ((g-n-s (cddr g-n-s)))
354 (cond ((integer? g-n-s)
355 (proc (standard-get g-n-s) (standard-set g-n-s)))
356 ((not (memq (slot-definition-allocation slotdef)
357 '(#:class #:each-subclass)))
358 (proc (car g-n-s) (cadr g-n-s))))))
360 (slot-ref class 'getters-n-setters)))
362 (define (access-for-each proc class)
363 (for-each (lambda (slotdef g-n-s)
364 (let ((g-n-s (cddr g-n-s))
365 (a (slot-definition-accessor slotdef)))
366 (cond ((integer? g-n-s)
367 (proc (slot-definition-name slotdef)
368 (and a (generic-function-name a))
370 (standard-set g-n-s)))
371 ((not (memq (slot-definition-allocation slotdef)
372 '(#:class #:each-subclass)))
373 (proc (slot-definition-name slotdef)
374 (and a (generic-function-name a))
378 (slot-ref class 'getters-n-setters)))
381 (procedure->memoizing-macro
383 "(restore CLASS (SLOT-NAME1 ...) EXP1 ...)"
384 `(let ((o (,%allocate-instance ,(cadr exp) '())))
385 (for-each (lambda (name val)
386 (,slot-set! o name val))
388 (list ,@(cdddr exp)))
391 (define-method (enumerate! (o <object>) env)
392 (get-set-for-each (lambda (get set)
394 (if (not (unbound? val))
395 (enumerate-component! val env))))
399 (define-method (write-readably (o <object>) file env)
400 (let ((class (class-of o)))
401 (display "(restore " file)
402 (display (class-name class) file)
405 (filter (lambda (slotdef)
406 (not (or (memq (slot-definition-allocation slotdef)
407 '(#:class #:each-subclass))
408 (and (slot-bound? o (slot-definition-name slotdef))
410 (slot-ref o (slot-definition-name slotdef))
412 (class-slots class))))
413 (if (not (null? slotdefs))
415 (display (slot-definition-name (car slotdefs)) file)
416 (for-each (lambda (slotdef)
417 (display #\space file)
418 (display (slot-definition-name slotdef) file))
421 (access-for-each (lambda (name aname get set)
422 (display #\space file)
424 (cond ((unbound? val)
425 (display '(make-unbound) file))
426 ((excluded? val env))
428 (if (literal? val env)
432 `(set! (,aname ,o) ,val)
433 `(slot-set! ,o ',name ,val))
442 ;;; Currently, we don't support reading in class objects
445 (define-method (enumerate! (o <class>) env) #f)
447 (define-method (write-readably (o <class>) file env)
448 (display (class-name o) file))
454 ;;; Currently, we don't support reading in generic functions
457 (define-method (enumerate! (o <generic>) env) #f)
459 (define-method (write-readably (o <generic>) file env)
460 (display (generic-function-name o) file))
466 ;;; Currently, we don't support reading in methods
469 (define-method (enumerate! (o <method>) env) #f)
471 (define-method (write-readably (o <method>) file env)
472 (goops-error "No read-syntax for <method> defined"))
478 (define-class <environment> ()
479 (object-info #:accessor object-info
480 #:init-form (make-hash-table 61))
481 (excluded #:accessor excluded
482 #:init-form (make-hash-table 61))
483 (pass-2? #:accessor pass-2?
485 (ref-stack #:accessor ref-stack
487 (objects #:accessor objects
489 (pre-defines #:accessor pre-defines
491 (locals #:accessor locals
493 (stand-ins #:accessor stand-ins
495 (post-defines #:accessor post-defines
497 (patchers #:accessor patchers
499 (multiple-bound #:accessor multiple-bound
503 (define-method (initialize (env <environment>) initargs)
505 (cond ((get-keyword #:excluded initargs #f)
506 => (lambda (excludees)
507 (for-each (lambda (e)
508 (hashq-create-handle! (excluded env) e #f))
511 (define-method (object-info o env)
512 (hashq-ref (object-info env) o))
514 (define-method ((setter object-info) o env x)
515 (hashq-set! (object-info env) o x))
517 (define (excluded? o env)
518 (hashq-get-handle (excluded env) o))
520 (define (add-patcher! patcher env)
521 (set! (patchers env) (cons patcher (patchers env))))
523 (define (push-ref! o env)
524 (set! (ref-stack env) (cons o (ref-stack env))))
526 (define (pop-ref! env)
527 (set! (ref-stack env) (cdr (ref-stack env))))
529 (define (container env)
530 (car (ref-stack env)))
532 (define-class <object-info> ()
533 (visiting #:accessor visiting
535 (binding #:accessor binding
537 (literal? #:accessor literal?
541 (define visiting? visiting)
543 (define-method (binding (info <boolean>))
546 (define-method (binding o env)
547 (binding (object-info o env)))
549 (define binding? binding)
551 (define-method (literal? (info <boolean>))
554 ;;; Note that this method is intended to be used only during the
557 (define-method (literal? o env)
560 (let ((info (object-info o env)))
561 ;; write-component sets all bindings first to #:defining,
563 (and (or (not (binding? info))
564 ;; we might be using `literal?' in a write-readably method
565 ;; to query about the object being defined
566 (and (eq? (visiting info) #:defining)
567 (null? (cdr (ref-stack env)))))
574 ;;; Enumeration has two passes.
576 ;;; Pass 1: Detect common substructure, circular references and order
578 ;;; Pass 2: Detect literals
580 (define (enumerate-component! o env)
581 (cond ((immediate? o) #t)
583 ((excluded? o env) #t)
585 (let ((info (object-info o env)))
587 ;; if circular reference, we print as a literal
588 ;; (note that during pass-2, circular references are
589 ;; forward references, i.e. *not* yet marked with #:pass-2
590 (not (eq? (visiting? info) #:pass-2))
591 (and (enumerate! o env)
593 (set! (literal? info) #t)
597 (set! (binding info) #t)
599 ;; circular reference--mark container
600 (set! (binding (object-info (container env) env)) #t))))
602 (let ((info (make <object-info>)))
603 (set! (object-info o env) info)
605 (set! (visiting? info) #t)
607 (set! (visiting? info) #f)
609 (set! (objects env) (cons o (objects env)))))))
611 (define (write-component-procedure o file env)
612 "Return #f if circular reference"
613 (cond ((immediate? o) (write o file) #t)
614 ((readable? o) (write (readable-expression o) file) #t)
615 ((excluded? o env) (display #f file) #t)
617 (let ((info (object-info o env)))
618 (cond ((not (binding? info)) (write-readably o file env) #t)
619 ((not (eq? (visiting info) #:defined)) #f) ;forward reference
620 (else (display (binding info) file) #t))))))
622 ;;; write-component OBJECT PATCHER FILE ENV
624 (define write-component
625 (procedure->memoizing-macro
627 `(or (write-component-procedure ,(cadr exp) ,@(cdddr exp))
629 (display #f ,(cadddr exp))
630 (add-patcher! ,(caddr exp) env))))))
636 (define binding-name car)
637 (define binding-object cdr)
639 (define (pass-1! alist env)
640 ;; Determine object order and necessary bindings
641 (for-each (lambda (binding)
642 (enumerate-component! (binding-object binding) env))
645 (define (make-local i)
646 (string->symbol (string-append "%o" (number->string i))))
648 (define (name-bindings! alist env)
649 ;; Name top-level bindings
650 (for-each (lambda (b)
651 (let ((o (binding-object b)))
652 (if (not (or (immediate? o)
655 (let ((info (object-info o env)))
656 (if (symbol? (binding info))
657 ;; already bound to a variable
658 (set! (multiple-bound env)
659 (acons (binding info)
661 (multiple-bound env)))
663 (binding-name b)))))))
665 ;; Name rest of bindings and create stand-in and definition lists
666 (let post-loop ((ls (objects env))
668 (cond ((or (null? ls)
669 (eq? (binding (car ls) env) #t))
670 (set! (post-defines env) post-defs)
671 (set! (objects env) ls))
672 ((not (binding (car ls) env))
673 (post-loop (cdr ls) post-defs))
675 (post-loop (cdr ls) (cons (car ls) post-defs)))))
676 (let pre-loop ((ls (reverse (objects env)))
683 (set! (pre-defines env) (reverse pre-defs))
684 (set! (locals env) (reverse locs))
685 (set! (stand-ins env) (reverse sins)))
686 (let ((info (object-info (car ls) env)))
687 (cond ((not (binding? info))
688 (pre-loop (cdr ls) i pre-defs locs sins))
689 ((boolean? (binding info))
691 (set! (binding info) (make-local i))
700 (cons (car ls) pre-defs)
704 (let ((real-name (binding info)))
705 (set! (binding info) (make-local i))
710 (acons (binding info) real-name sins)))))))))
712 (define (pass-2! env)
713 (set! (pass-2? env) #t)
714 (for-each (lambda (o)
715 (let ((info (object-info o env)))
716 (set! (literal? info) (enumerate! o env))
717 (set! (visiting info) #:pass-2)))
718 (append (pre-defines env)
720 (post-defines env))))
722 (define (write-define! name val literal? file)
723 (display "(define " file)
725 (display #\space file)
726 (if literal? (display #\' file))
728 (display ")\n" file))
730 (define (write-empty-defines! file env)
731 (for-each (lambda (stand-in)
732 (write-define! (cdr stand-in) #f #f file))
734 (for-each (lambda (o)
735 (write-define! (binding o env) #f #f file))
738 (define (write-definition! prefix o file env)
739 (display prefix file)
740 (let ((info (object-info o env)))
741 (display (binding info) file)
742 (display #\space file)
746 (set! (visiting info) #:defining)
747 (write-readably o file env)
748 (set! (visiting info) #:defined)
752 (define (write-let*-head! file env)
753 (display "(let* (" file)
754 (write-definition! "(" (car (locals env)) file env)
755 (for-each (lambda (o)
756 (write-definition! "\n (" o file env))
758 (display ")\n" file))
760 (define (write-rebindings! prefix bindings file env)
761 (for-each (lambda (patch)
762 (display prefix file)
763 (display (cdr patch) file)
764 (display #\space file)
765 (display (car patch) file)
766 (display ")\n" file))
769 (define (write-definitions! selector prefix file env)
770 (for-each (lambda (o)
771 (write-definition! prefix o file env)
775 (define (write-patches! prefix file env)
776 (for-each (lambda (patch)
777 (display prefix file)
778 (display (let name-objects ((patcher patch))
779 (cond ((binding patcher env)
781 (cond ((assq name (stand-ins env))
785 (cons (name-objects (car patcher))
786 (name-objects (cdr patcher))))
790 (reverse (patchers env))))
792 (define (write-immediates! alist file)
793 (for-each (lambda (b)
794 (if (immediate? (binding-object b))
795 (write-define! (binding-name b)
801 (define (write-readables! alist file env)
803 (for-each (lambda (b)
804 (cond ((not (readable? (binding-object b))))
805 ((assq (binding-object b) written)
807 (set! (multiple-bound env)
810 (multiple-bound env)))))
812 (write-define! (binding-name b)
813 (readable-expression (binding-object b))
816 (set! written (acons (binding-object b)
821 (define-method (save-objects (alist <pair>) (file <string>) . rest)
822 (let ((port (open-output-file file)))
823 (apply save-objects alist port rest)
827 (define-method (save-objects (alist <pair>) (file <output-port>) . rest)
828 (let ((excluded (if (>= (length rest) 1) (car rest) '()))
829 (uses (if (>= (length rest) 2) (cadr rest) '())))
830 (let ((env (make <environment> #:excluded excluded)))
832 (name-bindings! alist env)
834 (if (not (null? uses))
836 (write `(use-modules ,@uses) file)
838 (write-immediates! alist file)
839 (if (null? (locals env))
841 (write-definitions! post-defines "(define " file env)
842 (write-patches! "" file env))
844 (write-definitions! pre-defines "(define " file env)
845 (write-empty-defines! file env)
846 (write-let*-head! file env)
847 (write-rebindings! " (set! " (stand-ins env) file env)
848 (write-definitions! post-defines " (set! " file env)
849 (write-patches! " " file env)
850 (display " )\n" file)))
851 (write-readables! alist file env)
852 (write-rebindings! "(define " (reverse (multiple-bound env)) file env))))
854 (define-method (load-objects (file <string>))
855 (let* ((port (open-input-file file))
856 (objects (load-objects port)))
860 (define-method (load-objects (file <input-port>))
861 (let ((m (make-module)))
862 (module-use! m the-scm-module)
863 (module-use! m %module-public-interface)
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)))