3 ;;;; Copyright (C) 2000, 2001 Free Software Foundation, Inc.
5 ;;;; This program is free software; you can redistribute it and/or modify
6 ;;;; it under the terms of the GNU General Public License as published by
7 ;;;; the Free Software Foundation; either version 2, or (at your option)
8 ;;;; any later version.
10 ;;;; This program 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
13 ;;;; GNU General Public License for more details.
15 ;;;; You should have received a copy of the GNU General Public License
16 ;;;; along with this software; see the file COPYING. If not, write to
17 ;;;; the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 ;;;; Boston, MA 02111-1307 USA
20 ;;;; As a special exception, the Free Software Foundation gives permission
21 ;;;; for additional uses of the text contained in its release of GUILE.
23 ;;;; The exception is that, if you link the GUILE library with other files
24 ;;;; to produce an executable, this does not by itself cause the
25 ;;;; resulting executable to be covered by the GNU General Public License.
26 ;;;; Your use of that executable is in no way restricted on account of
27 ;;;; linking the GUILE library code into it.
29 ;;;; This exception does not however invalidate any other reasons why
30 ;;;; the executable file might be covered by the GNU General Public License.
32 ;;;; This exception applies only to the code released by the
33 ;;;; Free Software Foundation under the name GUILE. If you copy
34 ;;;; code from other Free Software Foundation releases into a copy of
35 ;;;; GUILE, as the General Public License permits, the exception does
36 ;;;; not apply to the code that you add in this way. To avoid misleading
37 ;;;; anyone as to the status of such modified files, you must delete
38 ;;;; this exception notice from them.
40 ;;;; If you write modifications of your own for GUILE, it is your choice
41 ;;;; whether to permit this exception to apply to your modifications.
42 ;;;; If you do not wish that, delete this exception notice.
46 (define-module (oop goops save)
47 :use-module (oop goops internal)
48 :use-module (oop goops util)
51 (re-export make-unbound)
53 (export save-objects load-objects restore
54 enumerate! enumerate-component!
55 write-readably write-component write-component-procedure
56 literal? readable make-readable)
59 ;;; save-objects ALIST PORT [EXCLUDED] [USES]
61 ;;; ALIST ::= ((NAME . OBJECT) ...)
63 ;;; Save OBJECT ... to PORT so that when the data is read and evaluated
64 ;;; OBJECT ... are re-created under names NAME ... .
65 ;;; Exclude any references to objects in the list EXCLUDED.
66 ;;; Add a (use-modules . USES) line to the top of the saved text.
68 ;;; In some instances, when `save-object' doesn't know how to produce
69 ;;; readable syntax for an object, you can explicitly register read
70 ;;; syntax for an object using the special form `readable'.
74 ;;; The function `foo' produces an object of obscure structure.
75 ;;; Only `foo' can construct such objects. Because of this, an
78 ;;; (define x (vector 1 (foo)))
80 ;;; cannot be saved by `save-objects'. But if you instead write
82 ;;; (define x (vector 1 (readable (foo))))
84 ;;; `save-objects' will happily produce the necessary read syntax.
86 ;;; To add new read syntax, hang methods on `enumerate!' and
89 ;;; enumerate! OBJECT ENV
90 ;;; Should call `enumerate-component!' (which takes same args) on
91 ;;; each component object. Should return #t if the composite object
92 ;;; can be written as a literal. (`enumerate-component!' returns #t
93 ;;; if the component is a literal.
95 ;;; write-readably OBJECT PORT ENV
96 ;;; Should write a readable representation of OBJECT to PORT.
97 ;;; Should use `write-component' to print each component object.
98 ;;; Use `literal?' to decide if a component is a literal.
102 ;;; enumerate-component! OBJECT ENV
104 ;;; write-component OBJECT PATCHER PORT ENV
105 ;;; PATCHER is an expression which, when evaluated, stores OBJECT
106 ;;; into its current location.
110 ;;; (write-component (car ls) `(set-car! ,ls ,(car ls)) file env)
112 ;;; write-component is a macro.
114 ;;; literal? COMPONENT ENV
117 (define-method (immediate? (o <top>)) #f)
119 (define-method (immediate? (o <null>)) #t)
120 (define-method (immediate? (o <number>)) #t)
121 (define-method (immediate? (o <boolean>)) #t)
122 (define-method (immediate? (o <symbol>)) #t)
123 (define-method (immediate? (o <char>)) #t)
124 (define-method (immediate? (o <keyword>)) #t)
126 ;;; enumerate! OBJECT ENVIRONMENT
128 ;;; Return #t if object is a literal.
130 (define-method (enumerate! (o <top>) env) #t)
132 (define-method (write-readably (o <top>) file env)
133 ;;(goops-error "No read-syntax defined for object `~S'" o)
134 (write o file) ;doesn't catch bugs, but is much more flexible
141 (if (or (not (defined? 'readables))
143 (define readables (make-weak-key-hash-table 61)))
146 (procedure->memoizing-macro
148 `(make-readable ,(cadr exp) ',(copy-tree (cadr exp))))))
150 (define (make-readable obj expr)
151 (hashq-set! readables obj expr)
154 (define (readable-expression obj)
155 `(readable ,(hashq-ref readables obj)))
157 (define (readable? obj)
158 (hashq-get-handle readables obj))
164 (define-method (enumerate! (o <string>) env) #f)
170 (define-method (enumerate! (o <vector>) env)
171 (or (not (vector? o))
173 (array-for-each (lambda (o)
174 (if (not (enumerate-component! o env))
179 (define-method (write-readably (o <vector>) file env)
180 (if (not (vector? o))
182 (let ((n (vector-length o)))
185 (let ((not-literal? (not (literal? o env))))
186 (display (if not-literal?
190 (if (and not-literal?
191 (literal? (vector-ref o 0) env))
193 (write-component (vector-ref o 0)
194 `(vector-set! ,o 0 ,(vector-ref o 0))
199 (display #\space file)
200 (if (and not-literal?
201 (literal? (vector-ref o i) env))
203 (write-component (vector-ref o i)
204 `(vector-set! ,o ,i ,(vector-ref o i))
207 (display #\) file))))))
214 (define-method (enumerate! (o <array>) env)
215 (enumerate-component! (shared-array-root o) env))
217 (define (make-mapper array)
218 (let* ((dims (array-dimensions array))
219 (n (array-rank array))
220 (indices (reverse (if (<= n 11)
221 (list-tail '(t s r q p n m l k j i) (- 11 n))
227 (cons (gensym "i") ls))))))))
229 (+ ,(shared-array-offset array)
230 ,@(map (lambda (ind dim inc)
231 `(* ,inc ,(if (pair? dim) `(- ,ind ,(car dim)) ind)))
233 (array-dimensions array)
234 (shared-array-increments array))))))
236 (define (write-array prefix o not-literal? file env)
237 (letrec ((inner (lambda (n indices)
239 (let ((el (apply array-ref o
240 (reverse (cons 0 indices)))))
241 (if (and not-literal?
246 `(array-set! ,o ,el ,@indices)
251 (display #\space file)
252 (let ((el (apply array-ref o
253 (reverse (cons i indices)))))
254 (if (and not-literal?
259 `(array-set! ,o ,el ,@indices)
262 (display prefix file)
263 (let loop ((dims (array-dimensions o))
265 (cond ((null? (cdr dims))
266 (inner (car dims) indices))
268 (let ((n (car dims)))
272 (display #\space file))
273 (display prefix file)
274 (loop (cdr dims) (cons i indices))
275 (display #\) file))))))
278 (define-method (write-readably (o <array>) file env)
279 (let ((root (shared-array-root o)))
280 (cond ((literal? o env)
281 (if (not (vector? root))
285 (display (array-rank o) file)
286 (write-array #\( o #f file env))))
288 (display "(make-shared-array " file)
289 (if (literal? root env)
291 (write-component root
292 (goops-error "write-readably(<array>): internal error")
295 (display #\space file)
296 (display (make-mapper o) file)
297 (for-each (lambda (dim)
298 (display #\space file)
300 (array-dimensions o))
303 (display "(list->uniform-array " file)
304 (display (array-rank o) file)
305 (display " '() " file)
306 (write-array "(list " o file env)))))
312 ;;; These methods have more complex structure than is required for
313 ;;; most objects, since they take over some of the logic of
314 ;;; `write-component'.
317 (define-method (enumerate! (o <pair>) env)
318 (let ((literal? (enumerate-component! (car o) env)))
319 (and (enumerate-component! (cdr o) env)
322 (define-method (write-readably (o <pair>) file env)
323 (let ((proper? (let loop ((ls o))
326 (not (binding? (cdr ls) env))
328 (1? (or (not (pair? (cdr o)))
329 (binding? (cdr o) env)))
330 (not-literal? (not (literal? o env)))
332 (refs (ref-stack env)))
333 (display (cond ((not not-literal?) #\()
338 (if (and not-literal?
339 (literal? (car o) env))
341 (write-component (car o) `(set-car! ,o ,(car o)) file env)
342 (do ((ls (cdr o) (cdr ls))
344 ((or (not (pair? ls))
348 (if (not not-literal?)
350 (display #\space file)
351 (if (and not-literal?
354 (write-component ls `(set-cdr! ,prev ,ls) file env)))
356 (display #\space file)
357 (set! infos (cons (object-info ls env) infos))
358 (push-ref! ls env) ;*fixme* optimize
359 (set! (visiting? (car infos)) #t)
360 (if (and not-literal?
361 (literal? (car ls) env))
363 (write-component (car ls) `(set-car! ,ls ,(car ls)) file env)
365 (for-each (lambda (info)
366 (set! (visiting? info) #f))
368 (set! (ref-stack env) refs)
375 ;;; Doesn't yet handle unbound slots
377 ;; Don't export this function! This is all very temporary.
379 (define (get-set-for-each proc class)
380 (for-each (lambda (slotdef g-n-s)
381 (let ((g-n-s (cddr g-n-s)))
382 (cond ((integer? g-n-s)
383 (proc (standard-get g-n-s) (standard-set g-n-s)))
384 ((not (memq (slot-definition-allocation slotdef)
385 '(#:class #:each-subclass)))
386 (proc (car g-n-s) (cadr g-n-s))))))
388 (slot-ref class 'getters-n-setters)))
390 (define (access-for-each proc class)
391 (for-each (lambda (slotdef g-n-s)
392 (let ((g-n-s (cddr g-n-s))
393 (a (slot-definition-accessor slotdef)))
394 (cond ((integer? g-n-s)
395 (proc (slot-definition-name slotdef)
396 (and a (generic-function-name a))
398 (standard-set g-n-s)))
399 ((not (memq (slot-definition-allocation slotdef)
400 '(#:class #:each-subclass)))
401 (proc (slot-definition-name slotdef)
402 (and a (generic-function-name a))
406 (slot-ref class 'getters-n-setters)))
411 "(restore CLASS (SLOT-NAME1 ...) EXP1 ...)"
412 `(let ((o (,%allocate-instance ,(cadr exp) '())))
413 (for-each (lambda (name val)
414 (,slot-set! o name val))
416 (list ,@(cdddr exp)))
419 (define-method (enumerate! (o <object>) env)
420 (get-set-for-each (lambda (get set)
422 (if (not (unbound? val))
423 (enumerate-component! val env))))
427 (define-method (write-readably (o <object>) file env)
428 (let ((class (class-of o)))
429 (display "(restore " file)
430 (display (class-name class) file)
433 (filter (lambda (slotdef)
434 (not (or (memq (slot-definition-allocation slotdef)
435 '(#:class #:each-subclass))
436 (and (slot-bound? o (slot-definition-name slotdef))
438 (slot-ref o (slot-definition-name slotdef))
440 (class-slots class))))
441 (if (not (null? slotdefs))
443 (display (slot-definition-name (car slotdefs)) file)
444 (for-each (lambda (slotdef)
445 (display #\space file)
446 (display (slot-definition-name slotdef) file))
449 (access-for-each (lambda (name aname get set)
450 (display #\space file)
452 (cond ((unbound? val)
453 (display '(make-unbound) file))
454 ((excluded? val env))
456 (if (literal? val env)
460 `(set! (,aname ,o) ,val)
461 `(slot-set! ,o ',name ,val))
470 ;;; Currently, we don't support reading in class objects
473 (define-method (enumerate! (o <class>) env) #f)
475 (define-method (write-readably (o <class>) file env)
476 (display (class-name o) file))
482 ;;; Currently, we don't support reading in generic functions
485 (define-method (enumerate! (o <generic>) env) #f)
487 (define-method (write-readably (o <generic>) file env)
488 (display (generic-function-name o) file))
494 ;;; Currently, we don't support reading in methods
497 (define-method (enumerate! (o <method>) env) #f)
499 (define-method (write-readably (o <method>) file env)
500 (goops-error "No read-syntax for <method> defined"))
506 (define-class <environment> ()
507 (object-info #:accessor object-info
508 #:init-form (make-hash-table 61))
509 (excluded #:accessor excluded
510 #:init-form (make-hash-table 61))
511 (pass-2? #:accessor pass-2?
513 (ref-stack #:accessor ref-stack
515 (objects #:accessor objects
517 (pre-defines #:accessor pre-defines
519 (locals #:accessor locals
521 (stand-ins #:accessor stand-ins
523 (post-defines #:accessor post-defines
525 (patchers #:accessor patchers
527 (multiple-bound #:accessor multiple-bound
531 (define-method (initialize (env <environment>) initargs)
533 (cond ((get-keyword #:excluded initargs #f)
534 => (lambda (excludees)
535 (for-each (lambda (e)
536 (hashq-create-handle! (excluded env) e #f))
539 (define-method (object-info o env)
540 (hashq-ref (object-info env) o))
542 (define-method ((setter object-info) o env x)
543 (hashq-set! (object-info env) o x))
545 (define (excluded? o env)
546 (hashq-get-handle (excluded env) o))
548 (define (add-patcher! patcher env)
549 (set! (patchers env) (cons patcher (patchers env))))
551 (define (push-ref! o env)
552 (set! (ref-stack env) (cons o (ref-stack env))))
554 (define (pop-ref! env)
555 (set! (ref-stack env) (cdr (ref-stack env))))
557 (define (container env)
558 (car (ref-stack env)))
560 (define-class <object-info> ()
561 (visiting #:accessor visiting
563 (binding #:accessor binding
565 (literal? #:accessor literal?
569 (define visiting? visiting)
571 (define-method (binding (info <boolean>))
574 (define-method (binding o env)
575 (binding (object-info o env)))
577 (define binding? binding)
579 (define-method (literal? (info <boolean>))
582 ;;; Note that this method is intended to be used only during the
585 (define-method (literal? o env)
588 (let ((info (object-info o env)))
589 ;; write-component sets all bindings first to #:defining,
591 (and (or (not (binding? info))
592 ;; we might be using `literal?' in a write-readably method
593 ;; to query about the object being defined
594 (and (eq? (visiting info) #:defining)
595 (null? (cdr (ref-stack env)))))
602 ;;; Enumeration has two passes.
604 ;;; Pass 1: Detect common substructure, circular references and order
606 ;;; Pass 2: Detect literals
608 (define (enumerate-component! o env)
609 (cond ((immediate? o) #t)
611 ((excluded? o env) #t)
613 (let ((info (object-info o env)))
615 ;; if circular reference, we print as a literal
616 ;; (note that during pass-2, circular references are
617 ;; forward references, i.e. *not* yet marked with #:pass-2
618 (not (eq? (visiting? info) #:pass-2))
619 (and (enumerate! o env)
621 (set! (literal? info) #t)
625 (set! (binding info) #t)
627 ;; circular reference--mark container
628 (set! (binding (object-info (container env) env)) #t))))
630 (let ((info (make <object-info>)))
631 (set! (object-info o env) info)
633 (set! (visiting? info) #t)
635 (set! (visiting? info) #f)
637 (set! (objects env) (cons o (objects env)))))))
639 (define (write-component-procedure o file env)
640 "Return #f if circular reference"
641 (cond ((immediate? o) (write o file) #t)
642 ((readable? o) (write (readable-expression o) file) #t)
643 ((excluded? o env) (display #f file) #t)
645 (let ((info (object-info o env)))
646 (cond ((not (binding? info)) (write-readably o file env) #t)
647 ((not (eq? (visiting info) #:defined)) #f) ;forward reference
648 (else (display (binding info) file) #t))))))
650 ;;; write-component OBJECT PATCHER FILE ENV
652 (define write-component
653 (procedure->memoizing-macro
655 `(or (write-component-procedure ,(cadr exp) ,@(cdddr exp))
657 (display #f ,(cadddr exp))
658 (add-patcher! ,(caddr exp) env))))))
664 (define binding-name car)
665 (define binding-object cdr)
667 (define (pass-1! alist env)
668 ;; Determine object order and necessary bindings
669 (for-each (lambda (binding)
670 (enumerate-component! (binding-object binding) env))
673 (define (make-local i)
674 (string->symbol (string-append "%o" (number->string i))))
676 (define (name-bindings! alist env)
677 ;; Name top-level bindings
678 (for-each (lambda (b)
679 (let ((o (binding-object b)))
680 (if (not (or (immediate? o)
683 (let ((info (object-info o env)))
684 (if (symbol? (binding info))
685 ;; already bound to a variable
686 (set! (multiple-bound env)
687 (acons (binding info)
689 (multiple-bound env)))
691 (binding-name b)))))))
693 ;; Name rest of bindings and create stand-in and definition lists
694 (let post-loop ((ls (objects env))
696 (cond ((or (null? ls)
697 (eq? (binding (car ls) env) #t))
698 (set! (post-defines env) post-defs)
699 (set! (objects env) ls))
700 ((not (binding (car ls) env))
701 (post-loop (cdr ls) post-defs))
703 (post-loop (cdr ls) (cons (car ls) post-defs)))))
704 (let pre-loop ((ls (reverse (objects env)))
711 (set! (pre-defines env) (reverse pre-defs))
712 (set! (locals env) (reverse locs))
713 (set! (stand-ins env) (reverse sins)))
714 (let ((info (object-info (car ls) env)))
715 (cond ((not (binding? info))
716 (pre-loop (cdr ls) i pre-defs locs sins))
717 ((boolean? (binding info))
719 (set! (binding info) (make-local i))
728 (cons (car ls) pre-defs)
732 (let ((real-name (binding info)))
733 (set! (binding info) (make-local i))
738 (acons (binding info) real-name sins)))))))))
740 (define (pass-2! env)
741 (set! (pass-2? env) #t)
742 (for-each (lambda (o)
743 (let ((info (object-info o env)))
744 (set! (literal? info) (enumerate! o env))
745 (set! (visiting info) #:pass-2)))
746 (append (pre-defines env)
748 (post-defines env))))
750 (define (write-define! name val literal? file)
751 (display "(define " file)
753 (display #\space file)
754 (if literal? (display #\' file))
756 (display ")\n" file))
758 (define (write-empty-defines! file env)
759 (for-each (lambda (stand-in)
760 (write-define! (cdr stand-in) #f #f file))
762 (for-each (lambda (o)
763 (write-define! (binding o env) #f #f file))
766 (define (write-definition! prefix o file env)
767 (display prefix file)
768 (let ((info (object-info o env)))
769 (display (binding info) file)
770 (display #\space file)
774 (set! (visiting info) #:defining)
775 (write-readably o file env)
776 (set! (visiting info) #:defined)
780 (define (write-let*-head! file env)
781 (display "(let* (" file)
782 (write-definition! "(" (car (locals env)) file env)
783 (for-each (lambda (o)
784 (write-definition! "\n (" o file env))
786 (display ")\n" file))
788 (define (write-rebindings! prefix bindings file env)
789 (for-each (lambda (patch)
790 (display prefix file)
791 (display (cdr patch) file)
792 (display #\space file)
793 (display (car patch) file)
794 (display ")\n" file))
797 (define (write-definitions! selector prefix file env)
798 (for-each (lambda (o)
799 (write-definition! prefix o file env)
803 (define (write-patches! prefix file env)
804 (for-each (lambda (patch)
805 (display prefix file)
806 (display (let name-objects ((patcher patch))
807 (cond ((binding patcher env)
809 (cond ((assq name (stand-ins env))
813 (cons (name-objects (car patcher))
814 (name-objects (cdr patcher))))
818 (reverse (patchers env))))
820 (define (write-immediates! alist file)
821 (for-each (lambda (b)
822 (if (immediate? (binding-object b))
823 (write-define! (binding-name b)
829 (define (write-readables! alist file env)
831 (for-each (lambda (b)
832 (cond ((not (readable? (binding-object b))))
833 ((assq (binding-object b) written)
835 (set! (multiple-bound env)
838 (multiple-bound env)))))
840 (write-define! (binding-name b)
841 (readable-expression (binding-object b))
844 (set! written (acons (binding-object b)
849 (define-method (save-objects (alist <pair>) (file <string>) . rest)
850 (let ((port (open-output-file file)))
851 (apply save-objects alist port rest)
855 (define-method (save-objects (alist <pair>) (file <output-port>) . rest)
856 (let ((excluded (if (>= (length rest) 1) (car rest) '()))
857 (uses (if (>= (length rest) 2) (cadr rest) '())))
858 (let ((env (make <environment> #:excluded excluded)))
860 (name-bindings! alist env)
862 (if (not (null? uses))
864 (write `(use-modules ,@uses) file)
866 (write-immediates! alist file)
867 (if (null? (locals env))
869 (write-definitions! post-defines "(define " file env)
870 (write-patches! "" file env))
872 (write-definitions! pre-defines "(define " file env)
873 (write-empty-defines! file env)
874 (write-let*-head! file env)
875 (write-rebindings! " (set! " (stand-ins env) file env)
876 (write-definitions! post-defines " (set! " file env)
877 (write-patches! " " file env)
878 (display " )\n" file)))
879 (write-readables! alist file env)
880 (write-rebindings! "(define " (reverse (multiple-bound env)) file env))))
882 (define-method (load-objects (file <string>))
883 (let* ((port (open-input-file file))
884 (objects (load-objects port)))
888 (define-method (load-objects (file <input-port>))
889 (let ((m (make-module)))
890 (module-use! m the-scm-module)
891 (module-use! m %module-public-interface)
892 (save-module-excursion
894 (set-current-module m)
895 (let loop ((sexp (read file)))
896 (if (not (eof-object? sexp))
899 (loop (read file)))))))
900 (module-map (lambda (name var)
901 (cons name (variable-ref var)))