(scm_init_rw_builtins): Renamed from scm_init_rw. Return
[bpt/guile.git] / ice-9 / boot-9.scm
index 6722348..6ff5cf5 100644 (file)
@@ -1,31 +1,49 @@
 ;;; installed-scm-file
 
-;;;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000 Free Software Foundation, Inc.
-;;;; 
+;;;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
+;;;;
 ;;;; This program is free software; you can redistribute it and/or modify
 ;;;; it under the terms of the GNU General Public License as published by
 ;;;; the Free Software Foundation; either version 2, or (at your option)
 ;;;; any later version.
-;;;; 
+;;;;
 ;;;; This program is distributed in the hope that it will be useful,
 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 ;;;; GNU General Public License for more details.
-;;;; 
+;;;;
 ;;;; You should have received a copy of the GNU General Public License
 ;;;; along with this software; see the file COPYING.  If not, write to
 ;;;; the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 ;;;; Boston, MA 02111-1307 USA
-;;;; 
+;;;;
 \f
 
+;;; Commentary:
+
 ;;; This file is the first thing loaded into Guile.  It adds many mundane
 ;;; definitions and a few that are interesting.
 ;;;
-;;; The module system (hence the hierarchical namespace) are defined in this 
+;;; The module system (hence the hierarchical namespace) are defined in this
 ;;; file.
 ;;;
 
+;;; Code:
+
+\f
+;;; {Deprecation}
+;;;
+
+;; We don't have macros here, but we do want to define
+;; `begin-deprecated' early.
+
+(define begin-deprecated
+  (procedure->memoizing-macro
+   (lambda (exp env)
+     (if (include-deprecated-features)
+        `(begin ,@(cdr exp))
+        `#f))))
+
 \f
 ;;; {Features}
 ;;
 ;;; {Trivial Functions}
 ;;;
 
-(define (id x) x)
+(define (identity x) x)
 (define (1+ n) (+ n 1))
-(define (-1+ n) (+ n -1))
-(define 1- -1+)
-(define return-it noop)
+(define (1- n) (+ n -1))
 (define (and=> value procedure) (and value (procedure value)))
 (define (make-hash-table k) (make-vector k '()))
 
-;;; apply-to-args is functionally redunant with apply and, worse,
+(begin-deprecated
+ (define (id x)
+   (issue-deprecation-warning "`id' is deprecated.  Use `identity' instead.")
+   (identity x))
+ (define (-1+ n)
+   (issue-deprecation-warning "`-1+' is deprecated.  Use `1-' instead.")
+   (1- n))
+ (define (return-it . args)
+   (issue-deprecation-warning "`return-it' is deprecated.  Use `noop' instead.")
+   (apply noop args)))
+
+;;; apply-to-args is functionally redundant with apply and, worse,
 ;;; is less general than apply since it only takes two arguments.
 ;;;
-;;; On the other hand, apply-to-args is a syntacticly convenient way to 
+;;; On the other hand, apply-to-args is a syntacticly convenient way to
 ;;; perform binding in many circumstances when the "let" family of
 ;;; of forms don't cut it.  E.g.:
 ;;;
 ;;;    (apply-to-args (return-3d-mouse-coords)
-;;;      (lambda (x y z) 
+;;;      (lambda (x y z)
 ;;;            ...))
 ;;;
 
 (define (apply-to-args args fn) (apply fn args))
 
 \f
+
 ;;; {Integer Math}
 ;;;
 
                                (if (even? k) acc (proc acc x))
                                proc))))
 
-(define string-character-length string-length)
-
-
-
-;; A convenience function for combining flag bits.  Like logior, but
-;; handles the cases of 0 and 1 arguments.
-;;
-(define (flags . args)
-  (cond
-   ((null? args) 0)
-   ((null? (cdr args)) (car args))
-   (else (apply logior args))))
+(begin-deprecated
+ (define (string-character-length s)
+   (issue-deprecation-warning "`string-character-length' is deprecated.  Use `string-length' instead.")
+   (string-length s))
+ (define (flags . args)
+   (issue-deprecation-warning "`flags' is deprecated.  Use `logior' instead.")
+   (apply logior args)))
 
 \f
 ;;; {Symbol Properties}
        (symbol-pset! sym (delq! pair (symbol-pref sym))))))
 
 ;;; {General Properties}
+;;;
 
 ;; This is a more modern interface to properties.  It will replace all
 ;; other property-like things eventually.
       new-port))
 
 ;; 0: type-name, 1: fields
-(define record-type-vtable 
+(define record-type-vtable
   (make-vtable-vtable "prpr" 0
                      (lambda (s p)
                        (cond ((eq? s record-type-vtable)
 ;; Apply f to successive elements of l until exhaustion or f returns #f.
 ;; If returning early, return #f.  Otherwise, return the last value returned
 ;; by f.  If f has never been called because l is empty, return #t.
-;; 
+;;
 (define (and-map f lst)
   (let loop ((result #t)
             (l lst))
     (if (pair? maybe-fd)
        (set-port-revealed! port 1))
     port))
-  
+
 (define (dup->inport port/fd . maybe-fd)
   (apply dup->port port/fd "r" maybe-fd))
 
           (display help)
           (newline))))
    kw-desc))
-                 
 
-          
+
+
 (define (transform-usage-lambda cases)
   (let* ((raw-usage (delq! 'else (map car cases)))
         (usage-sans-specials (map (lambda (x)
       ;; to maximally one module.
       (set-procedure-property! closure 'module module))))
 
-;;; This procedure is depreated
-;;;
-(define eval-in-module eval)
+(begin-deprecated
+ (define (eval-in-module exp mod)
+   (issue-deprecation-warning
+    "`eval-in-module' is deprecated.  Use `eval' instead.")
+   (eval exp mod)))
 
 \f
 ;;; {Observer protocol}
 ;;;
 
 ;; module-search fn m
-;; 
+;;
 ;; return the first non-#f result of FN applied to M and then to
 ;; the modules in the uses of m, and so on recursively.  If all applications
 ;; return #f, then so does this function.
 
 ;;; {Is a symbol interned in a module?}
 ;;;
-;;; Symbol S in Module M is interned if S occurs in 
+;;; Symbol S in Module M is interned if S occurs in
 ;;; of S in M has been set to some well-defined value.
 ;;;
 ;;; It is possible to intern a symbol in a module without providing
   ((if (symbol? key) hashq-remove! hash-remove!) ob key))
 
 ;; module-symbol-locally-interned? module symbol
-;; 
+;;
 ;; is a symbol interned (not neccessarily defined) locally in a given module
 ;; or its uses?  Interned symbols shadow inherited bindings even if
 ;; they are not themselves bound to a defined value.
   (not (not (module-obarray-get-handle (module-obarray m) v))))
 
 ;; module-symbol-interned? module symbol
-;; 
+;;
 ;; is a symbol interned (not neccessarily defined) anywhere in a given module
 ;; or its uses?  Interned symbols shadow inherited bindings even if
 ;; they are not themselves bound to a defined value.
 ;))
 
 ;; module-variable module symbol
-;; 
-;; like module-local-variable, except search the uses in the 
+;;
+;; like module-local-variable, except search the uses in the
 ;; case V is not found in M.
 ;;
 ;; NOTE: This function is superseded with C code (see modules.c)
 ;;;
 
 ;; module-symbol-binding module symbol opt-value
-;; 
+;;
 ;; return the binding of a variable specified by name within
 ;; a given module, signalling an error if the variable is unbound.
 ;; If the OPT-VALUE is passed, then instead of signalling an error,
            (error "Locally unbound variable." v)))))
 
 ;; module-symbol-binding module symbol opt-value
-;; 
+;;
 ;; return the binding of a variable specified by name within
 ;; a given module, signalling an error if the variable is unbound.
 ;; If the OPT-VALUE is passed, then instead of signalling an error,
 
 
 ;; module-make-local-var! module symbol
-;; 
+;;
 ;; ensure a variable for V in the local namespace of M.
 ;; If no variable was already there, then create a new and uninitialzied
 ;; variable.
       (and (module-binder m)
           ((module-binder m) m v #t))
       (begin
-       (let ((answer (make-undefined-variable v)))
+       (let ((answer (make-undefined-variable)))
+         (variable-set-name-hint! answer v)
          (module-obarray-set! (module-obarray m) v answer)
          (module-modified m)
          answer))))
 
 ;; module-add! module symbol var
-;; 
+;;
 ;; ensure a particular variable for V in the local namespace of M.
 ;;
 (define (module-add! m v var)
   (module-obarray-set! (module-obarray m) v var)
   (module-modified m))
 
-;; module-remove! 
-;; 
+;; module-remove!
+;;
 ;; make sure that a symbol is undefined in the local namespace of M.
 ;;
 (define (module-remove! m v)
   (module-modified m))
 
 ;; MODULE-FOR-EACH -- exported
-;; 
+;;
 ;; Call PROC on each symbol in MODULE, with arguments of (SYMBOL VARIABLE).
 ;;
 (define (module-for-each proc module)
 ;;; {Low Level Bootstrapping}
 ;;;
 
-;; make-root-module 
+;; make-root-module
 
-;; A root module uses the symhash table (the system's privileged 
-;; obarray).  Being inside a root module is like using SCM without
-;; any module system.
+;; A root module uses the pre-modules-obarray as its obarray.  This
+;; special obarray accumulates all bindings that have been established
+;; before the module system is fully booted.
 ;;
-
-
-(define (root-module-closure m s define?)
-  (let ((bi (builtin-variable s)))
-    (and bi
-        (or define? (variable-bound? bi))
-        (begin
-          (module-add! m s bi)
-          bi))))
+;; (The obarray continues to be used by code that has been closed over
+;;  before the module system has been booted.)
 
 (define (make-root-module)
-  (make-module 1019 '() root-module-closure))
-
-
-;; make-scm-module 
+  (let ((m (make-module 0)))
+    (set-module-obarray! m (%get-pre-modules-obarray))
+    m))
 
-;; An scm module is a module into which the lazy binder copies
-;; variable bindings from the system symhash table.  The mapping is
-;; one way only; newly introduced bindings in an scm module are not
-;; copied back into the system symhash table (and can be used to override
-;; bindings from the symhash table).
-;;
+;; make-scm-module
 
-(define (scm-module-closure m s define?)
-  (let ((bi (builtin-variable s)))
-    (and bi
-        (variable-bound? bi)
-        (begin
-          (module-add! m s bi)
-          bi))))
+;; The root interface is a module that uses the same obarray as the
+;; root module.  It does not allow new definitions, tho.
 
 (define (make-scm-module)
-  (make-module 1019 '() scm-module-closure))
-
+  (let ((m (make-module 0)))
+    (set-module-obarray! m (%get-pre-modules-obarray))
+    (set-module-eval-closure! m (standard-interface-eval-closure m))
+    m))
 
 
-;; the-module
-;;
-;; NOTE: This binding is used in libguile/modules.c.
-;;
-(define the-module (make-fluid))
-
-;; scm:eval-transformer
-;;
-;;(define scm:eval-transformer (make-fluid)) ; initialized in eval.c.
-
-;; set-current-module module
-;;
-;; set the current module as viewed by the normalizer.
-;;
-;; NOTE: This binding is used in libguile/modules.c.
-;;
-(define (set-current-module m)
-  (fluid-set! the-module m)
-  (if m
-      (begin
-       ;; *top-level-lookup-closure* is now deprecated
-       (fluid-set! *top-level-lookup-closure*
-                   (module-eval-closure (fluid-ref the-module)))
-       (fluid-set! scm:eval-transformer (module-transformer (fluid-ref the-module))))
-      (fluid-set! *top-level-lookup-closure* #f)))
-
-
-;; current-module
-;;
-;; return the current module as viewed by the normalizer.
-;;
-(define (current-module) (fluid-ref the-module))
 \f
 ;;; {Module-based Loading}
 ;;;
 ;; Returns the value of a variable called NAME in MODULE or any of its
 ;; used modules.  If there is no such variable, then if the optional third
 ;; argument DEFAULT is present, it is returned; otherwise an error is signaled.
-;; 
+;;
 (define (module-ref module name . rest)
   (let ((variable (module-variable module name)))
     (if (and variable (variable-bound? variable))
 ;;
 ;; Sets the variable called NAME in MODULE (or in a module that MODULE uses)
 ;; to VALUE; if there is no such variable, an error is signaled.
-;; 
+;;
 (define (module-set! module name value)
   (let ((variable (module-variable module name)))
     (if variable
 ;;
 ;; Sets the variable called NAME in MODULE to VALUE; if there is no such
 ;; variable, it is added first.
-;; 
+;;
 (define (module-define! module name value)
   (let ((variable (module-local-variable module name)))
     (if variable
        (begin
          (variable-set! variable value)
          (module-modified module))
-       (module-add! module name (make-variable value name)))))
+       (let ((variable (make-variable value)))
+         (variable-set-name-hint! variable name)
+         (module-add! module name variable)))))
 
 ;; MODULE-DEFINED? -- exported
 ;;
 ;; MODULE-USE! module interface
 ;;
 ;; Add INTERFACE to the list of interfaces used by MODULE.
-;; 
+;;
 (define (module-use! module interface)
   (set-module-uses! module
                    (cons interface (delq! interface (module-uses module))))
 ;;; modules.
 ;;;
 ;;;            (nested-ref some-root-module '(foo bar baz))
-;;;            => <value of a variable named baz in the module bound to bar in 
+;;;            => <value of a variable named baz in the module bound to bar in
 ;;;                the module bound to foo in some-root-module>
 ;;;
 ;;;
 (set-module-kind! the-scm-module 'interface)
 (for-each set-system-module! (list the-root-module the-scm-module) '(#t #t))
 
-(set-current-module the-root-module)
-
-(define app (make-module 31))
-(local-define '(app modules) (make-module 31))
-(local-define '(app modules guile) the-root-module)
-
-;; (define-special-value '(app modules new-ws) (lambda () (make-scm-module)))
+;; NOTE: This binding is used in libguile/modules.c.
+;;
+(define (make-modules-in module name)
+  (if (null? name)
+      module
+      (cond
+       ((module-ref module (car name) #f)
+       => (lambda (m) (make-modules-in m (cdr name))))
+       (else   (let ((m (make-module 31)))
+                 (set-module-kind! m 'directory)
+                 (set-module-name! m (append (or (module-name module)
+                                                 '())
+                                             (list (car name))))
+                 (module-define! module (car name) m)
+                 (make-modules-in m (cdr name)))))))
 
-(define (try-load-module name)
-  (or (try-module-linked name)
-      (try-module-autoload name)
-      (try-module-dynamic-link name)))
+(define (beautify-user-module! module)
+  (let ((interface (module-public-interface module)))
+    (if (or (not interface)
+           (eq? interface module))
+       (let ((interface (make-module 31)))
+         (set-module-name! interface (module-name module))
+         (set-module-kind! interface 'interface)
+         (set-module-public-interface! module interface))))
+  (if (and (not (memq the-scm-module (module-uses module)))
+          (not (eq? module the-root-module)))
+      (set-module-uses! module (append (module-uses module) (list the-scm-module)))))
 
 ;; NOTE: This binding is used in libguile/modules.c.
 ;;
       (if already
          ;; The module already exists...
          (if (and (or (null? maybe-autoload) (car maybe-autoload))
-                  (not (module-ref already '%module-public-interface #f)))
+                  (not (module-public-interface already)))
              ;; ...but we are told to load and it doesn't contain source, so
              (begin
                (try-load-module name)
                (try-load-module name))
            ;; Get/create it.
            (make-modules-in (current-module) full-name))))))
-           
-(define (beautify-user-module! module)
-  (let ((interface (module-public-interface module)))
-    (if (or (not interface)
-           (eq? interface module))
-       (let ((interface (make-module 31)))
-         (set-module-name! interface (module-name module))
-         (set-module-kind! interface 'interface)
-         (set-module-public-interface! module interface))))
-  (if (and (not (memq the-scm-module (module-uses module)))
-          (not (eq? module the-root-module)))
-      (set-module-uses! module (append (module-uses module) (list the-scm-module)))))
+
+;; Cheat.
+(define try-module-autoload #f)
+
+;; This boots the module system.  All bindings needed by modules.c
+;; must have been defined by now.
+;;
+(set-current-module the-root-module)
+
+(define app (make-module 31))
+(local-define '(app modules) (make-module 31))
+(local-define '(app modules guile) the-root-module)
+
+;; (define-special-value '(app modules new-ws) (lambda () (make-scm-module)))
+
+(define (try-load-module name)
+  (or (try-module-linked name)
+      (try-module-autoload name)
+      (try-module-dynamic-link name)))
 
 (define (purify-module! module)
   "Removes bindings in MODULE which are inherited from the (guile) module."
             (eq? (car (last-pair use-list)) the-scm-module))
        (set-module-uses! module (reverse (cdr (reverse use-list)))))))
 
-;; NOTE: This binding is used in libguile/modules.c.
-;;
-(define (make-modules-in module name)
-  (if (null? name)
-      module
-      (cond
-       ((module-ref module (car name) #f)
-       => (lambda (m) (make-modules-in m (cdr name))))
-       (else   (let ((m (make-module 31)))
-                 (set-module-kind! m 'directory)
-                 (set-module-name! m (append (or (module-name module)
-                                                 '())
-                                             (list (car name))))
-                 (module-define! module (car name) m)
-                 (make-modules-in m (cdr name)))))))
-
-(define (resolve-interface name)
-  (let ((module (resolve-module name)))
-    (and module (module-public-interface module))))
-
-
-(define %autoloader-developer-mode #t)
+;; Return a module interface made from SPEC.
+;; SPEC can be a list of symbols, in which case it names a module
+;; whose public interface is found and returned.
+;;
+;; SPEC can also be of the form:
+;;  (MODULE-NAME [:select SELECTION] [:rename RENAMER])
+;; in which case a partial interface is newly created and returned.
+;; MODULE-NAME is a list of symbols, as above; SELECTION is a list of
+;; binding-specs to be imported; and RENAMER is a procedure that takes a
+;; symbol and returns its new name.  A binding-spec is either a symbol or a
+;; pair of symbols (ORIG . SEEN), where ORIG is the name in the used module
+;; and SEEN is the name in the using module.  Note that SEEN is also passed
+;; through RENAMER.
+;;
+;; The `:select' and `:rename' clauses are optional.  If both are omitted, the
+;; returned interface has no bindings.  If the `:select' clause is omitted,
+;; RENAMER operates on the used module's public interface.
+;;
+;; Signal "no code for module" error if module name is not resolvable or its
+;; public interface is not available.  Signal "no binding" error if selected
+;; binding does not exist in the used module.
+;;
+(define (resolve-interface spec)
+  (let* ((simple? (not (pair? (car spec))))
+         (name (if simple? spec (car spec)))
+         (module (resolve-module name))
+         (public-i (and module (module-public-interface module))))
+    (and (or (not module) (not public-i))
+         (error "no code for module" name))
+    (if simple?
+        public-i
+        (let ((selection (cond ((memq ':select spec) => cadr)
+                               (else (module-map (lambda (sym var) sym)
+                                                 public-i))))
+              (rename (cond ((memq ':rename spec)
+                             => (lambda (x)
+                                  ;; fixme:ttn -- move to macroexpansion time
+                                  (eval (cadr x) (current-module))))
+                            (else identity)))
+              (custom-i (make-module 31)))
+          (set-module-kind! custom-i 'interface)
+          (for-each (lambda (bspec)
+                      (let* ((direct? (symbol? bspec))
+                             (orig (if direct? bspec (car bspec)))
+                             (seen (if direct? bspec (cdr bspec))))
+                        (module-add! custom-i (rename seen)
+                                     (or (module-local-variable module orig)
+                                         (error
+                                          ;; fixme: format manually for now
+                                          (simple-format
+                                           #f "no binding `~A' in module ~A"
+                                           orig name))))))
+                    selection)
+          custom-i))))
+
+(define (symbol-prefix-proc prefix)
+  (lambda (symbol)
+    (symbol-append prefix symbol)))
 
 (define (process-define-module args)
-  (let*  ((module-id (car args))
-         (module (resolve-module module-id #f))
-         (kws (cdr args)))
+  (let* ((module-id (car args))
+         (module (resolve-module module-id #f))
+         (kws (cdr args))
+         (unrecognized (lambda (arg)
+                         (error "unrecognized define-module argument" arg))))
     (beautify-user-module! module)
     (let loop ((kws kws)
               (reversed-interfaces '())
          (begin
            (for-each (lambda (interface)
                        (module-use! module interface))
-                     reversed-interfaces)
+                     (reverse reversed-interfaces))
            (module-export! module exports))
          (let ((keyword (if (keyword? (car kws))
                             (keyword->symbol (car kws))
                                         (string->symbol (substring s 1))))))))
            (case keyword
              ((use-module use-syntax)
-              (if (not (pair? (cdr kws)))
-                  (error "unrecognized defmodule argument" kws))
-              (let* ((used-name (cadr kws))
-                     (used-module (resolve-module used-name)))
-                (if (not (module-ref used-module
-                                     '%module-public-interface
-                                     #f))
-                    (begin
-                      ((if %autoloader-developer-mode warn error)
-                       "no code for module" (module-name used-module))
-                      (beautify-user-module! used-module)))
-                (let ((interface (module-public-interface used-module)))
-                  (if (not interface)
-                      (error "missing interface for use-module"
-                             used-module))
-                  (if (eq? keyword 'use-syntax)
-                      (set-module-transformer!
-                       module
-                       (module-ref interface (car (last-pair used-name))
-                                   #f)))
-                  (loop (cddr kws)
-                        (cons interface reversed-interfaces)
-                        exports))))
+              (or (pair? (cdr kws))
+                  (unrecognized kws))
+               (let* ((spec (cadr kws))
+                      (interface (resolve-interface spec)))
+                 (and (eq? keyword 'use-syntax)
+                      (or (symbol? (car spec))
+                          (error "invalid module name for use-syntax"
+                                 spec))
+                      (set-module-transformer!
+                       module
+                       (module-ref interface (car (last-pair module-name))
+                                   #f)))
+                 (loop (cddr kws)
+                       (cons interface reversed-interfaces)
+                       exports)))
              ((autoload)
-              (if (not (and (pair? (cdr kws)) (pair? (cddr kws))))
-                  (error "unrecognized defmodule argument" kws))
+              (or (and (pair? (cdr kws)) (pair? (cddr kws)))
+                   (unrecognized kws))
               (loop (cdddr kws)
                     (cons (make-autoload-interface module
                                                    (cadr kws)
               (purify-module! module)
               (loop (cdr kws) reversed-interfaces exports))
              ((export)
-              (if (not (pair? (cdr kws)))
-                  (error "unrecognized defmodule argument" kws))
+              (or (pair? (cdr kws))
+                   (unrecognized kws))
               (loop (cddr kws)
                     reversed-interfaces
                     (append (cadr kws) exports)))
-             (else     
-              (error "unrecognized defmodule argument" kws))))))
+             (else
+               (unrecognized kws))))))
     (set-current-module module)
     module))
 
     (module-constructor #() '() b #f #f name 'autoload
                        '() (make-weak-value-hash-table 31) 0)))
 
+;;; {Compiled module}
+
+(define load-compiled #f)
+
 \f
 ;;; {Autoloading modules}
 
     (resolve-module dir-hint-module-name #f)
     (and (not (autoload-done-or-in-progress? dir-hint name))
         (let ((didit #f))
+          (define (load-file proc file)
+            (save-module-excursion (lambda () (proc file)))
+            (set! didit #t))
           (dynamic-wind
            (lambda () (autoload-in-progress! dir-hint name))
            (lambda ()
-             (let ((full (%search-load-path (in-vicinity dir-hint name))))
-               (if full
-                   (begin
-                     (save-module-excursion (lambda () (primitive-load full)))
-                     (set! didit #t)))))
+             (let ((file (in-vicinity dir-hint name)))
+               (cond ((and load-compiled
+                           (%search-load-path (string-append file ".go")))
+                      => (lambda (full)
+                           (load-file load-compiled full)))
+                     ((%search-load-path file)
+                      => (lambda (full)
+                           (load-file primitive-load full))))))
            (lambda () (set-autoloaded! dir-hint name didit)))
           didit))))
 
 \f
 ;;; Dynamic linking of modules
 
-;; Initializing a module that is written in C is a two step process.
-;; First the module's `module init' function is called.  This function
-;; is expected to call `scm_register_module_xxx' to register the `real
-;; init' function.  Later, when the module is referenced for the first
-;; time, this real init function is called in the right context.  See
-;; gtcltk-lib/gtcltk-module.c for an example.
-;;
-;; The code for the module can be in a regular shared library (so that
-;; the `module init' function will be called when libguile is
-;; initialized).  Or it can be dynamically linked.
-;;
-;; You can safely call `scm_register_module_xxx' before libguile
-;; itself is initialized.  You could call it from an C++ constructor
-;; of a static object, for example.
-;;
-;; To make your Guile extension into a dynamic linkable module, follow
-;; these easy steps:
-;;
-;; - Find a name for your module, like (ice-9 gtcltk)
-;; - Write a function with a name like
-;;
-;;     scm_init_ice_9_gtcltk_module
-;;
-;;   This is your `module init' function.  It should call
-;;   
-;;     scm_register_module_xxx ("ice-9 gtcltk", scm_init_gtcltk);
-;;   
-;;   "ice-9 gtcltk" is the C version of the module name. Slashes are
-;;   replaced by spaces, the rest is untouched. `scm_init_gtcltk' is
-;;   the real init function that executes the usual initializations
-;;   like making new smobs, etc.
-;;
-;; - Make a shared library with your code and a name like
-;;
-;;     ice-9/libgtcltk.so
-;;
-;;   and put it somewhere in %load-path.
-;;
-;; - Then you can simply write `:use-module (ice-9 gtcltk)' and it
-;;   will be linked automatically.
-;;
-;; This is all very experimental.
+;; This method of dynamically linking Guile Extensions is deprecated.
+;; Use `dynamic-link' and `dynamic-call' explicitely from Scheme code
+;; instead.
+
+;; XXX - We can not offer the removal of this code thru the
+;; deprecation mechanism since we have no complete replacement yet.
 
 (define (split-c-module-name str)
   (let loop ((rev '())
        (append! (convert-c-registered-modules dynobj)
                 registered-modules)))
 
+(define (warn-autoload-deprecation modname)
+  ;; Do nothing here until we can deprecate the code for real.
+  (if #f
+      (issue-deprecation-warning
+       "Autoloading of compiled code modules is deprecated."
+       "Write a Scheme file instead that uses `dynamic-link' directly.")))
+
 (define (init-dynamic-module modname)
-  ;; Register any linked modules which has been registered on the C level
+  ;; Register any linked modules which have been registered on the C level
   (register-modules #f)
   (or-map (lambda (modinfo)
            (if (equal? (car modinfo) modname)
                (begin
+                 (warn-autoload-deprecation modname)
                  (set! registered-modules (delq! modinfo registered-modules))
                  (let ((mod (resolve-module modname #f)))
                    (save-module-excursion
                                       (string-append libname ".la"))))
     (and (file-exists? libtool-filename)
         libtool-filename)))
-                             
+
 (define (try-using-sharlib-name libdir libname)
   (in-vicinity libdir (string-append libname ".so")))
 
 
 
 
+\f
+;; {EVAL-CASE}
+;;
+;; (eval-case ((situation*) forms)* (else forms)?)
+;;
+;; Evaluate certain code based on the situation that eval-case is used
+;; in.  The only defined situation right now is `load-toplevel' which
+;; triggers for code evaluated at the top-level, for example from the
+;; REPL or when loading a file.
+
+(define eval-case
+  (procedure->memoizing-macro
+   (lambda (exp env)
+     (define (toplevel-env? env)
+       (or (not (pair? env)) (not (pair? (car env)))))
+     (define (syntax)
+       (error "syntax error in eval-case"))
+     (let loop ((clauses (cdr exp)))
+       (cond
+       ((null? clauses)
+        #f)
+       ((not (list? (car clauses)))
+        (syntax))
+       ((eq? 'else (caar clauses))
+        (or (null? (cdr clauses))
+            (syntax))
+        (cons 'begin (cdar clauses)))
+       ((not (list? (caar clauses)))
+        (syntax))
+       ((and (toplevel-env? env)
+             (memq 'load-toplevel (caar clauses)))
+        (cons 'begin (cdar clauses)))
+       (else
+        (loop (cdr clauses))))))))
 
 \f
 ;;; {Macros}
   (let ((defmacro-transformer
          (lambda (name parms . body)
            (let ((transformer `(lambda ,parms ,@body)))
-             `(define ,name
-                (,(lambda (transformer)
-                    (defmacro:transformer transformer))
-                 ,transformer))))))
+             `(eval-case
+               ((load-toplevel)
+                (define ,name (defmacro:transformer ,transformer)))
+               (else
+                (error "defmacro can only be used at the top level")))))))
     (defmacro:transformer defmacro-transformer)))
 
 (define defmacro:syntax-transformer
   (let ((status #f)
        (interactive #t))
     (define (loop first)
-      (let ((next 
+      (let ((next
             (catch #t
 
                    (lambda ()
                                       (with-traps
                                        (lambda ()
                                          (first)
-                                      
+
                                          ;; This line is needed because mark
                                          ;; doesn't do closures quite right.
                                          ;; Unreferenced locals should be
                                     (lambda () (mask-signals))))
 
                                  lazy-handler-dispatch))
-                   
+
                    (lambda (key . args)
                      (case key
                        ((quit)
                                  (apply bad-throw key args))))))))))
        (if next (loop next) status)))
     (set! set-batch-mode?! (lambda (arg)
-                            (cond (arg 
+                            (cond (arg
                                    (set! interactive #f)
                                    (restore-signals))
                                   (#t
              the-last-stack
              (case (stack-id #t)
                ((repl-stack)
-                (apply make-stack #t save-stack eval #t 0 narrowing))
+                (apply make-stack #t save-stack primitive-eval #t 0 narrowing))
                ((load-stack)
                 (apply make-stack #t save-stack 0 #t 0 narrowing))
                ((tk-stack)
 ;;      (display "No backtrace available.\n")))
 
 (define (error-catching-repl r e p)
-  (error-catching-loop (lambda () (p (e (r))))))
+  (error-catching-loop
+   (lambda ()
+     (call-with-values (lambda () (e (r)))
+       (lambda the-values (for-each p the-values))))))
 
 (define (gc-run-time)
   (cdr (assq 'gc-time-taken (gc-stats))))
 
 (define before-read-hook (make-hook))
 (define after-read-hook (make-hook))
+(define before-eval-hook (make-hook 1))
+(define after-eval-hook (make-hook 1))
+(define before-print-hook (make-hook 1))
+(define after-print-hook (make-hook 1))
 
 ;;; The default repl-reader function.  We may override this if we've
 ;;; the readline library.
 
           (-eval (lambda (sourc)
                    (repl-report-start-timing)
-                   (start-stack 'repl-stack
-                                (primitive-eval sourc))))
+                   (run-hook before-eval-hook sourc)
+                   (let ((val (start-stack 'repl-stack
+                                           ;; If you change this procedure
+                                           ;; (primitive-eval), please also
+                                           ;; modify the repl-stack case in
+                                           ;; save-stack so that stack cutting
+                                           ;; continues to work.
+                                           (primitive-eval sourc))))
+                     (run-hook after-eval-hook sourc)
+                     val)))
+
 
           (-print (let ((maybe-print (lambda (result)
                                        (if (or scm-repl-print-unspecified
                     (lambda (result)
                       (if (not scm-repl-silent)
                           (begin
+                            (run-hook before-print-hook result)
                             (maybe-print result)
+                            (run-hook after-print-hook result)
                             (if scm-repl-verbose
                                 (repl-report))
                             (force-output))))))
                                       -eval
                                       -print)))
       (-quit status))))
-  
+
 
 \f
 ;;; {IOTA functions: generating lists of numbers}
         (if (symbol? first)
             (car rest)
             `(lambda ,(cdr first) ,@rest))))
-    `(define ,name (defmacro:transformer ,transformer))))
+    `(eval-case
+      ((load-toplevel)
+       (define ,name (defmacro:transformer ,transformer)))
+      (else
+       (error "define-macro can only be used at the top level")))))
 
 
 (defmacro define-syntax-macro (first . rest)
         (if (symbol? first)
             (car rest)
             `(lambda ,(cdr first) ,@rest))))
-    `(define ,name (defmacro:syntax-transformer ,transformer))))
-
-;; EVAL-WHEN
-;;
-;; (eval-when ((situation*) forms)* (else forms)?)
-;;
-;; Evaluate certain code based on the situation that eval-when is used
-;; in.  The only defined situation right now is `load-toplevel' which
-;; triggers for code evaluated at the top-level, for example from the
-;; REPL or when loading a file.
-(define eval-when
-  (procedure->memoizing-macro
-   (lambda (exp env)
-     (define (toplevel-env? env)
-       (or (not (pair? env)) (not (pair? (car env)))))
-     (define (syntax)
-       (error "syntax error in eval-when"))
-     (let loop ((clauses (cdr exp)))
-       (cond 
-       ((null? clauses)
-        #f)
-       ((not (list? (car clauses)))
-        (syntax))
-       ((eq? 'else (caar clauses))
-        (or (null? (cdr clauses))
-            (syntax))
-        (cons 'begin (cdar clauses)))
-       ((not (list? (caar clauses)))
-        (syntax))
-       ((and (toplevel-env? env)
-             (memq 'load-toplevel (caar clauses)))
-        (cons 'begin (cdar clauses)))
-       (else
-        (loop (cdr clauses))))))))
+    `(eval-case
+      ((load-toplevel)
+       (define ,name (defmacro:syntax-transformer ,transformer)))
+      (else
+       (error "define-syntax-macro can only be used at the top level")))))
 
 \f
 ;;; {Module System Macros}
 ;;;
 
 (defmacro define-module args
-  `(eval-when
+  `(eval-case
     ((load-toplevel)
      (process-define-module ',args))
     (else
 
 ;; the guts of the use-modules macro.  add the interfaces of the named
 ;; modules to the use-list of the current module, in order
-(define (process-use-modules module-names)
-  (for-each (lambda (module-name)
-             (let ((mod-iface (resolve-interface module-name)))
+(define (process-use-modules module-interface-specs)
+  (for-each (lambda (mif-spec)
+             (let ((mod-iface (resolve-interface mif-spec)))
                (or mod-iface
-                   (error "no such module" module-name))
+                   (error "no such module" mif-spec))
                (module-use! (current-module) mod-iface)))
-           (reverse module-names)))
+            module-interface-specs))
 
 (defmacro use-modules modules
-  `(eval-when
+  `(eval-case
     ((load-toplevel)
      (process-use-modules ',modules))
     (else
      (error "use-modules can only be used at the top level"))))
 
 (defmacro use-syntax (spec)
-  `(eval-when
+  `(eval-case
     ((load-toplevel)
      ,@(if (pair? spec)
           `((process-use-modules ',(list spec))
           `((set-module-transformer! (current-module) ,spec)))
      (fluid-set! scm:eval-transformer (module-transformer (current-module))))
     (else
-     (error "use-modules can only be used at the top level"))))
+     (error "use-syntax can only be used at the top level"))))
 
 (define define-private define)
 
    (#t
     (let ((name (defined-name (car args))))
       `(begin
-        (eval-when ((load-toplevel) (export ,name)))
+        (eval-case ((load-toplevel) (export ,name)))
         (define-private ,@args))))))
 
 (defmacro defmacro-public args
    (#t
     (let ((name (defined-name (car args))))
       `(begin
-        (eval-when ((load-toplevel) (export ,name)))
+        (eval-case ((load-toplevel) (export ,name)))
         (defmacro ,@args))))))
 
 (define (module-export! m names)
              names)))
 
 (defmacro export names
-  `(eval-when
+  `(eval-case
     ((load-toplevel)
      (module-export! (current-module) ',names))
     (else
 
 (define load load-module)
 
+\f
+
+;;; {`cond-expand' for SRFI-0 support.}
+;;;
+;;; This syntactic form expands into different commands or
+;;; definitions, depending on the features provided by the Scheme
+;;; implementation.
+;;;
+;;; Syntax:
+;;;
+;;; <cond-expand>
+;;;   --> (cond-expand <cond-expand-clause>+)
+;;;     | (cond-expand <cond-expand-clause>* (else <command-or-definition>))
+;;; <cond-expand-clause>
+;;;   --> (<feature-requirement> <command-or-definition>*)
+;;; <feature-requirement>
+;;;   --> <feature-identifier>
+;;;     | (and <feature-requirement>*)
+;;;     | (or <feature-requirement>*)
+;;;     | (not <feature-requirement>)
+;;; <feature-identifier>
+;;;   --> <a symbol which is the name or alias of a SRFI>
+;;;
+;;; Additionally, this implementation provides the
+;;; <feature-identifier>s `guile' and `r5rs', so that programs can
+;;; determine the implementation type and the supported standard.
+;;;
+;;; Currently, the following feature identifiers are supported:
+;;;
+;;;   guile r5rs srfi-0
+;;;
+;;; Remember to update the features list when adding more SRFIs.
+
+(define cond-expand-features
+  ;; Adjust the above comment when changing this.
+  '(guile r5rs srfi-0))
+
+(define-macro (cond-expand clause . clauses)
+
+  (let ((clauses (cons clause clauses))
+       (syntax-error (lambda (cl)
+                       (error "invalid clause in `cond-expand'" cl))))
+    (letrec
+       ((test-clause
+         (lambda (clause)
+           (cond
+             ((symbol? clause)
+              (memq clause cond-expand-features))
+             ((pair? clause)
+              (cond
+                ((eq? 'and (car clause))
+                 (let lp ((l (cdr clause)))
+                   (cond ((null? l)
+                          #t)
+                         ((pair? l)
+                          (and (test-clause (car l)) (lp (cdr l))))
+                         (else
+                          (syntax-error clause)))))
+                ((eq? 'or (car clause))
+                 (let lp ((l (cdr clause)))
+                   (cond ((null? l)
+                          #f)
+                         ((pair? l)
+                          (or (test-clause (car l)) (lp (cdr l))))
+                         (else
+                          (syntax-error clause)))))
+                ((eq? 'not (car clause))
+                 (cond ((not (pair? (cdr clause)))
+                        (syntax-error clause))
+                       ((pair? (cddr clause))
+                        ((syntax-error clause))))
+                 (not (test-clause (cadr clause))))
+                (else
+                 (syntax-error clause))))
+             (else
+              (syntax-error clause))))))
+      (let lp ((c clauses))
+       (cond
+         ((null? c)
+          (error "Unfulfilled `cond-expand'"))
+         ((not (pair? c))
+          (syntax-error c))
+         ((not (pair? (car c)))
+          (syntax-error (car c)))
+         ((test-clause (caar c))
+          `(begin ,@(cdar c)))
+         ((eq? (caar c) 'else)
+          (if (pair? (cdr c))
+            (syntax-error c))
+          `(begin ,@(cdar c)))
+         (else
+          (lp (cdr c))))))))
+
+;; This procedure gets called from the startup code with a list of
+;; numbers, which are the numbers of the SRFIs to be loaded on startup.
+;;
+(define (use-srfis srfis)
+  (let lp ((s srfis))
+    (if (pair? s)
+        (let* ((srfi (string->symbol
+                      (string-append "srfi-" (number->string (car s)))))
+               (mod-i (resolve-interface (list 'srfi srfi))))
+          (module-use! (current-module) mod-i)
+          (set! cond-expand-features
+                (append cond-expand-features (list srfi)))
+          (lp (cdr s))))))
 
 \f
 
   (module-use! (resolve-module user) (resolve-module usee)))
 
 (define (load-emacs-interface)
-  (if (memq 'debug-extensions *features*)
-      (debug-enable 'backtrace))
+  (and (provided? 'debug-extensions)
+       (debug-enable 'backtrace))
   (named-module-use! '(guile-user) '(ice-9 emacs)))
 
 \f
       (lambda () (fluid-ref using-readline?))
       (lambda (v) (fluid-set! using-readline? v)))))
 
-(define (top-repl) 
+(define (top-repl)
 
   ;; Load emacs interface support if emacs option is given.
   (if (and (module-defined? the-root-module 'use-emacs-interface)
       (load-emacs-interface))
 
   ;; Place the user in the guile-user module.
-  (process-define-module 
+  (process-define-module
    '((guile-user)
      :use-module (guile)    ;so that bindings will be checked here first
      :use-module (ice-9 session)
      :use-module (ice-9 debug)
      :autoload (ice-9 debugger) (debug)))  ;load debugger on demand
-  (if (memq 'threads *features*)
-      (named-module-use! '(guile-user) '(ice-9 threads)))
-  (if (memq 'regex *features*)
-      (named-module-use! '(guile-user) '(ice-9 regex)))
+  (and (provided? 'threads)
+       (named-module-use! '(guile-user) '(ice-9 threads)))
+  (and (provided? 'regex)
+       (named-module-use! '(guile-user) '(ice-9 regex)))
 
   (let ((old-handlers #f)
        (signals (if (provided? 'posix)
 \f
 (define-module (guile))
 
-(append! %load-path (cons "." ()))
+(append! %load-path (cons "." '()))
 
+;;; boot-9.scm ends here