| 1 |
;;; |
| 2 |
;;; libmod.scm - modules, bindings, GLOCs |
| 3 |
;;; |
| 4 |
;;; Copyright (c) 2000-2024 Shiro Kawai <[email protected]> |
| 5 |
;;; |
| 6 |
;;; Redistribution and use in source and binary forms, with or without |
| 7 |
;;; modification, are permitted provided that the following conditions |
| 8 |
;;; are met: |
| 9 |
;;; |
| 10 |
;;; 1. Redistributions of source code must retain the above copyright |
| 11 |
;;; notice, this list of conditions and the following disclaimer. |
| 12 |
;;; |
| 13 |
;;; 2. Redistributions in binary form must reproduce the above copyright |
| 14 |
;;; notice, this list of conditions and the following disclaimer in the |
| 15 |
;;; documentation and/or other materials provided with the distribution. |
| 16 |
;;; |
| 17 |
;;; 3. Neither the name of the authors nor the names of its contributors |
| 18 |
;;; may be used to endorse or promote products derived from this |
| 19 |
;;; software without specific prior written permission. |
| 20 |
;;; |
| 21 |
;;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 22 |
;;; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 23 |
;;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 24 |
;;; A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 25 |
;;; OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 26 |
;;; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED |
| 27 |
;;; TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 28 |
;;; PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 29 |
;;; LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 30 |
;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 31 |
;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 |
;;; |
| 33 |
|
| 34 |
(select-module gauche) |
| 35 |
|
| 36 |
(inline-stub |
| 37 |
(.include "gauche/priv/configP.h" |
| 38 |
"gauche/vminsn.h" |
| 39 |
"gauche/priv/glocP.h" |
| 40 |
"gauche/priv/moduleP.h")) |
| 41 |
|
| 42 |
;;; |
| 43 |
;;; Modules API |
| 44 |
;;; |
| 45 |
|
| 46 |
(define-cproc module? (obj) ::<boolean> :constant SCM_MODULEP) |
| 47 |
|
| 48 |
(define-cproc module-name (mod::<module>) |
| 49 |
(return (SCM_OBJ (-> (SCM_MODULE mod) name)))) |
| 50 |
|
| 51 |
(define-cproc module-parents (mod::<module>) (return (-> mod parents))) |
| 52 |
(define-cproc module-precedence-list (mod::<module>) (return (-> mod mpl))) |
| 53 |
(define-cproc module-imports (mod::<module>) (return (-> mod imported))) |
| 54 |
(define-cproc module-exports (mod::<module>) Scm_ModuleExports) |
| 55 |
(define-cproc module-table (mod::<module>) (return (SCM_OBJ (-> mod internal)))) |
| 56 |
|
| 57 |
(define-cproc find-module (name::<symbol>) ::<module>? |
| 58 |
(return (Scm_FindModule name SCM_FIND_MODULE_QUIET))) |
| 59 |
|
| 60 |
(define-cproc all-modules () Scm_AllModules) |
| 61 |
|
| 62 |
(define-cproc make-module (name::<symbol>? :key (if-exists :error)) |
| 63 |
(let* ([error_if_exists::int TRUE]) |
| 64 |
(cond [(SCM_EQ if-exists ':error) (set! error_if_exists TRUE)] |
| 65 |
[(SCM_FALSEP if-exists) (set! error_if_exists FALSE)] |
| 66 |
[else (Scm_TypeError ":if-exists" ":error or #f" if_exists)]) |
| 67 |
(return (Scm_MakeModule name error_if_exists)))) |
| 68 |
|
| 69 |
;; (use x.y.z) ==> (requrie "x/y/z") (import x.y.z) |
| 70 |
(define-macro (use module . options) |
| 71 |
;; Kludge - remove these once we have low-level hygienic macro. |
| 72 |
(define begin. ((with-module gauche.internal make-identifier) |
| 73 |
'begin (find-module 'gauche) '())) |
| 74 |
(define require. ((with-module gauche.internal make-identifier) |
| 75 |
'require (find-module 'gauche) '())) |
| 76 |
(define import. ((with-module gauche.internal make-identifier) |
| 77 |
'import (find-module 'gauche) '())) |
| 78 |
`(,begin. |
| 79 |
(,require. ,(module-name->path module)) |
| 80 |
(,import. (,module ,@options)))) |
| 81 |
|
| 82 |
;; foo.bar.baz <=> "foo/bar/baz" |
| 83 |
;; - Two consecutive dots in module name becomes one dot in path |
| 84 |
;; foo..bar.baz <=> "foo.bar/baz". This is to support R7RS library |
| 85 |
;; whose name is (foo.bar baz). |
| 86 |
;; - Todo: Escape unsafe characters in name. |
| 87 |
(define (module-name->path name) |
| 88 |
(define (path-comp p) |
| 89 |
(rlet1 s (list->string (reverse p)) |
| 90 |
(cond [(equal? s "") |
| 91 |
(error "Invalid module name (it can't end with #\\.):" name)] |
| 92 |
[(or (equal? s ".") (equal? s "..")) |
| 93 |
(error "Invalid module name (component can't be \".\" or \"..\"):" |
| 94 |
name)]))) |
| 95 |
(with-input-from-string |
| 96 |
(if (identifier? name) |
| 97 |
(symbol->string (identifier->symbol name)) |
| 98 |
(error "identifier expected, but got:" name)) |
| 99 |
(^[] (let loop ([c (read-char)] [p '()] [ps '()]) |
| 100 |
(cond |
| 101 |
[(eof-object? c) |
| 102 |
(string-join (reverse (cons (path-comp p) ps)) "/")] |
| 103 |
[(eqv? c #\.) |
| 104 |
(let1 c2 (read-char) |
| 105 |
(if (eqv? c2 #\.) |
| 106 |
(loop (read-char) (cons c2 p) ps) |
| 107 |
(loop c2 '() (cons (path-comp p) ps))))] |
| 108 |
[else (loop (read-char) (cons c p) ps)]))))) |
| 109 |
|
| 110 |
(define (path->module-name path) |
| 111 |
(unless (string? path) (error "string required, but got:" path)) |
| 112 |
($ string->symbol $ (cut string-join <> ".") |
| 113 |
$ map (cut regexp-replace-all #/\./ <> "..") |
| 114 |
$ string-split path "/")) |
| 115 |
|
| 116 |
(inline-stub |
| 117 |
(define-cfn get-module-from-mod-or-name (id) ::ScmModule* :static |
| 118 |
(cond [(SCM_MODULEP id) (return (SCM_MODULE id))] |
| 119 |
[(SCM_SYMBOLP id) (return (Scm_FindModule (SCM_SYMBOL id) 0))] |
| 120 |
[(SCM_FALSEP id) (return (SCM_CURRENT_MODULE))] |
| 121 |
[else (Scm_Error "module or symbol required, but got: %S" id) |
| 122 |
(return NULL)])) |
| 123 |
) |
| 124 |
|
| 125 |
;; Global bindind access, public API |
| 126 |
(define-cproc module-binds? (mod-or-name name::<symbol>) ::<boolean> |
| 127 |
(return |
| 128 |
(not (SCM_UNBOUNDP |
| 129 |
(Scm_GlobalVariableRef (get-module-from-mod-or-name mod-or-name) |
| 130 |
name 0))))) |
| 131 |
(define-cproc module-exports? (mod-or-name name::<symbol>) ::<boolean> |
| 132 |
(return |
| 133 |
(not (SCM_UNBOUNDP |
| 134 |
(Scm_GlobalVariableRef (get-module-from-mod-or-name mod-or-name) |
| 135 |
name SCM_BINDING_EXTERNAL))))) |
| 136 |
(define-cproc module-binding-ref (mod_or_name name::<symbol> |
| 137 |
:optional |
| 138 |
fallback |
| 139 |
(stay-in-module::<boolean> #f)) |
| 140 |
(let* ([module::ScmModule* (get_module_from_mod_or_name mod_or_name)] |
| 141 |
[flags::int (?: stay_in_module SCM_BINDING_STAY_IN_MODULE 0)] |
| 142 |
[r (Scm_GlobalVariableRef module name flags)] |
| 143 |
[r2 (?: (SCM_UNBOUNDP r) fallback r)]) |
| 144 |
(when (SCM_UNBOUNDP r2) |
| 145 |
(Scm_Error "global identifier %S is not bound in module %S" |
| 146 |
name module)) |
| 147 |
(return r2))) |
| 148 |
|
| 149 |
;; Backward compatibility. Deprecated |
| 150 |
(define global-variable-bound? module-binds?) |
| 151 |
(define global-variable-ref module-binding-ref) |
| 152 |
|
| 153 |
(define-in-module gauche (symbol-bound? name :optional (module #f)) ; Deprecated |
| 154 |
(module-binds? module name)) |
| 155 |
|
| 156 |
;; Module import/export internal APIs. Not public. |
| 157 |
(select-module gauche.internal) |
| 158 |
(define-cproc %export-all (module::<module>) Scm_ExportAll) |
| 159 |
(define-cproc %extend-module (module::<module> supers::<list>) |
| 160 |
Scm_ExtendModule) |
| 161 |
|
| 162 |
;; Insert binding into the given module. flags may be a list of |
| 163 |
;; one or more of the following symbols: |
| 164 |
;; const - SCM_BINDING_CONST |
| 165 |
;; inlinable - SCM_BINDING_INLINABLE |
| 166 |
;; dummy - SCM_BINDING_DUMMY |
| 167 |
;; fresh - insert only if there's no binding yet (this isn't an |
| 168 |
;; attribute for the binding itself) |
| 169 |
;; returns GLOC or #f |
| 170 |
(define-cproc %insert-binding (mod::<module> name::<symbol> value |
| 171 |
:optional (flags '())) |
| 172 |
(let* ([z::int 0]) |
| 173 |
(if (and (not (SCM_FALSEP (Scm_Memq 'fresh flags))) |
| 174 |
(not (SCM_UNBOUNDP |
| 175 |
(Scm_GlobalVariableRef mod name |
| 176 |
SCM_BINDING_STAY_IN_MODULE)))) |
| 177 |
(return SCM_FALSE) |
| 178 |
(begin |
| 179 |
(unless (SCM_FALSEP (Scm_Memq 'const flags)) |
| 180 |
(logior= z SCM_BINDING_CONST)) |
| 181 |
(unless (SCM_FALSEP (Scm_Memq 'inlinable flags)) |
| 182 |
(logior= z SCM_BINDING_INLINABLE)) |
| 183 |
(unless (SCM_FALSEP (Scm_Memq 'dummy flags)) |
| 184 |
(logior= z SCM_BINDING_DUMMY)) |
| 185 |
(return (SCM_OBJ (Scm_MakeBinding mod name value z))))))) |
| 186 |
|
| 187 |
;; Insert binding as a syntactic keyword. VALUE must be #<macro> or #<syntax>. |
| 188 |
;; Currently Gauche conflates toplevel variable bindings and syntax bindings, |
| 189 |
;; so this is effectively the same as %insert-binding; but we may change it |
| 190 |
;; in future, so use this API to bind syntax. |
| 191 |
(define (%insert-syntax-binding mod name value) |
| 192 |
(unless (or (syntax? value) (macro? value)) |
| 193 |
(error "Syntax or macro object required, but got" value)) |
| 194 |
(%insert-binding mod name value)) |
| 195 |
|
| 196 |
(define-cproc %hide-binding (mod::<module> name::<symbol>) ::<void> |
| 197 |
Scm_HideBinding) |
| 198 |
|
| 199 |
(define-cproc %alias-binding (target::<module> tname::<symbol> |
| 200 |
origin::<module> oname::<symbol>) |
| 201 |
::<boolean> |
| 202 |
Scm_AliasBinding) |
| 203 |
|
| 204 |
(define-cproc %make-wrapper-module (origin::<module> prefix) |
| 205 |
Scm__MakeWrapperModule) |
| 206 |
|
| 207 |
(define-cproc %export-symbols (mod::<module> names) Scm_ExportSymbols) |
| 208 |
|
| 209 |
(define-cproc %import-module (mod::<module> imported |
| 210 |
:optional (prefix #f) |
| 211 |
(flags::<ulong> 0)) |
| 212 |
Scm_ImportModule) |
| 213 |
|
| 214 |
(define-cproc %import-modules (mod::<module> mods) ;deprecated |
| 215 |
Scm_ImportModules) |
| 216 |
|
| 217 |
;; sealing module. not public (yet) |
| 218 |
(define-cproc %seal-module! (mod::<module>) ::<void> |
| 219 |
Scm_ModuleSeal) |
| 220 |
|
| 221 |
(select-module gauche) |
| 222 |
(inline-stub |
| 223 |
(define-cfn module-print (obj port::ScmPort* _::ScmWriteContext*) |
| 224 |
::void :static |
| 225 |
(if (SCM_MODULEP (-> (SCM_MODULE obj) origin)) |
| 226 |
(Scm_Printf port "#<module %A$%A @%p>" |
| 227 |
(-> (SCM_MODULE obj) name) |
| 228 |
(-> (SCM_MODULE (-> (SCM_MODULE obj) origin)) name) |
| 229 |
obj) |
| 230 |
(Scm_Printf port "#<module %A>" (-> (SCM_MODULE obj) name)))) |
| 231 |
|
| 232 |
(define-cclass <module> |
| 233 |
"ScmModule*" "Scm_ModuleClass" |
| 234 |
(c "SCM_CLASS_COLLECTION_CPL") |
| 235 |
((name :setter #f) |
| 236 |
(mpl :setter #f) |
| 237 |
(parents :setter #f) |
| 238 |
(imports :c-name "imported" :setter #f) |
| 239 |
(exports :c-spec "Scm_ModuleExports(obj)" :setter #f) |
| 240 |
(export-all :type <boolean> :c-name "exportAll" :setter #f) |
| 241 |
(table :c-name "internal" :setter #f) |
| 242 |
(depends :c-name "depended" :setter #f) |
| 243 |
(origin :setter #f) |
| 244 |
(prefix :setter #f) |
| 245 |
(info)) |
| 246 |
(printer (c "module_print"))) |
| 247 |
) |
| 248 |
|
| 249 |
;;; |
| 250 |
;;; Universal import |
| 251 |
;;; |
| 252 |
|
| 253 |
(select-module gauche.internal) |
| 254 |
|
| 255 |
(declare (keep-private-macro import)) |
| 256 |
|
| 257 |
;; Kludge: We let user#import handle both gauche#import and r7rs#import, so |
| 258 |
;; that R7RS script can be run by gosh without any special treatment. |
| 259 |
;; If we see r7rs syntax, not only we import the specified module, but |
| 260 |
;; we switch the current module to r7rs.user. |
| 261 |
(define (%expand-user-import args) |
| 262 |
(if (any (^[import-spec] |
| 263 |
(or (symbol? import-spec) |
| 264 |
(and (list? import-spec) |
| 265 |
(pair? (cdr import-spec)) |
| 266 |
(keyword? (cadr import-spec))))) |
| 267 |
args) |
| 268 |
`((with-module gauche import) ,@args) |
| 269 |
(begin |
| 270 |
;; Some black magic needed here to inject the imported module to |
| 271 |
;; r7rs.user. We do this here instead of expanding into forms |
| 272 |
;; to do the work, for "select-module" thingy is tricky. |
| 273 |
(unless (provided? "r7rs-setup") |
| 274 |
(load "r7rs-setup") |
| 275 |
(provide "r7rs-setup")) |
| 276 |
(eval `(import ,@args) (find-module 'r7rs.user)) |
| 277 |
'(select-module r7rs.user)))) |
| 278 |
|
| 279 |
(select-module user) |
| 280 |
|
| 281 |
(define-macro (import . import-specs) |
| 282 |
((with-module gauche.internal %expand-user-import) import-specs)) |
| 283 |
|
| 284 |
;;; |
| 285 |
;;; GLOCs |
| 286 |
;;; |
| 287 |
|
| 288 |
(select-module gauche.internal) |
| 289 |
|
| 290 |
;; GLOCs are not for public use, so all APIs are internal. |
| 291 |
|
| 292 |
(inline-stub |
| 293 |
(declare-stub-type <gloc> "ScmGloc*" "GLOC" |
| 294 |
"SCM_GLOCP" "SCM_GLOC" "SCM_OBJ") |
| 295 |
) |
| 296 |
|
| 297 |
;; Returns GLOC object or #f. |
| 298 |
(define-cproc find-binding (mod::<module> name::<symbol> |
| 299 |
stay-in-module::<boolean>) |
| 300 |
::<gloc>? |
| 301 |
(return (Scm_FindBinding mod name |
| 302 |
(?: stay_in_module SCM_BINDING_STAY_IN_MODULE 0)))) |
| 303 |
|
| 304 |
;; This small piece of code encapsulates the common procedure in |
| 305 |
;; pass1/variable to find whether the variable reference is a constant |
| 306 |
;; or not. |
| 307 |
(define-cproc find-const-binding (id::<identifier>) |
| 308 |
(let* ([g::ScmGloc* (Scm_IdentifierGlobalBinding id)]) |
| 309 |
(cond [(or (not g) |
| 310 |
(not (SCM_GLOC_CONST_P g)) |
| 311 |
(SCM_VM_COMPILER_FLAG_IS_SET (Scm_VM) |
| 312 |
SCM_COMPILE_NOINLINE_CONSTS)) |
| 313 |
(return SCM_FALSE)] |
| 314 |
[else |
| 315 |
(return (SCM_GLOC_GET g))]))) |
| 316 |
|
| 317 |
(define-cproc gloc-bound? (gloc::<gloc>) ::<boolean> |
| 318 |
(return (not (SCM_UNBOUNDP (SCM_GLOC_GET gloc))))) |
| 319 |
(define-cproc gloc-ref (gloc::<gloc> :optional fallback) |
| 320 |
(let* ([v::ScmObj (SCM_GLOC_GET gloc)]) |
| 321 |
(if (SCM_UNBOUNDP v) |
| 322 |
(begin |
| 323 |
(when (SCM_UNBOUNDP fallback) |
| 324 |
(Scm_Error "gloc %S doesn't have a value" (SCM_OBJ gloc))) |
| 325 |
(return fallback)) |
| 326 |
(return v)))) |
| 327 |
(define-cproc gloc-set! (gloc::<gloc> value) SCM_GLOC_SET) |
| 328 |
(define-cproc gloc-const? (gloc::<gloc>) ::<boolean> Scm_GlocConstP) |
| 329 |
(define-cproc gloc-inlinable? (gloc::<gloc>) ::<boolean> Scm_GlocInlinableP) |
| 330 |
|
| 331 |
;;; |
| 332 |
;;; Identifier and binding |
| 333 |
;;; |
| 334 |
|
| 335 |
;; NB: Identifier procedures are in libsym.scm, for we might integrate |
| 336 |
;; symbols and identifiers (we're still not sure). |
| 337 |
|
| 338 |
;; Returns GLOC if id is bound to one, or #f. If GLOC is returned, |
| 339 |
;; it is always bound. |
| 340 |
|
| 341 |
;; (define (id->bound-gloc id) |
| 342 |
;; (and-let* ([gloc (find-binding (identifier-module id) |
| 343 |
;; (identifier-name id) #f)] |
| 344 |
;; [ (gloc-bound? gloc) ]) |
| 345 |
;; gloc)) |
| 346 |
(inline-stub |
| 347 |
(define-cfn Scm__IdentifierToBoundGloc (id::ScmIdentifier*) ::ScmGloc* |
| 348 |
(let* ([gloc::ScmGloc* (Scm_IdentifierGlobalBinding id)]) |
| 349 |
(if (and gloc (not (SCM_UNBOUNDP (SCM_GLOC_GET gloc)))) |
| 350 |
(return gloc) |
| 351 |
(return NULL)))) |
| 352 |
|
| 353 |
(define-cproc id->bound-gloc (id::<identifier>) ::<gloc>? |
| 354 |
Scm__IdentifierToBoundGloc)) |
| 355 |
|
| 356 |
;; Returns #t if id1 and id2 both refer to the same existing global binding. |
| 357 |
;; Like free-identifier=? but we know id1 and id2 are both toplevel and |
| 358 |
;; at least one is bound, so we skip local binding lookup. |
| 359 |
(define (global-identifier=? id1 id2) |
| 360 |
(and-let* ([ (wrapped-identifier? id1) ] |
| 361 |
[ (wrapped-identifier? id2) ] |
| 362 |
[g1 (id->bound-gloc id1)] |
| 363 |
[g2 (id->bound-gloc id2)]) |
| 364 |
(eq? g1 g2))) |