| 1 |
/* |
| 2 |
* module.h - Modules |
| 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 |
#ifndef GAUCHE_MODULE_H |
| 35 |
#define GAUCHE_MODULE_H |
| 36 |
|
| 37 |
/* |
| 38 |
* A module keeps "toplevel environment", which maps names of free |
| 39 |
* variables (symbols) to a location (GLOCs). |
| 40 |
* |
| 41 |
* Note on anonymous wrapper modules: Sometimes, the system |
| 42 |
* implicitly creates an anonymous module to wrap an original |
| 43 |
* module to manipulate visibility of bindings. |
| 44 |
*/ |
| 45 |
|
| 46 |
struct ScmModuleRec { |
| 47 |
SCM_HEADER; |
| 48 |
ScmObj name; /* symbol or #f */ |
| 49 |
ScmObj imported; /* list of imported modules. */ |
| 50 |
int exportAll; /* TRUE if (export-all) */ |
| 51 |
ScmObj parents; /* direct parent modules */ |
| 52 |
ScmObj mpl; /* module precedence list */ |
| 53 |
ScmObj depended; /* list of modules that are depended by this |
| 54 |
module for compilation */ |
| 55 |
ScmHashTable *internal; /* Symbol -> GLoc, looked up from this module |
| 56 |
itself and inherited ones */ |
| 57 |
ScmHashTable *external; /* Symbol -> GLoc, looked up from the modules |
| 58 |
that imports this module. This table only |
| 59 |
holds exported symbols (it may have |
| 60 |
different name if export renaming is in |
| 61 |
effect) */ |
| 62 |
ScmObj origin; /* if this module is an anonymous wrapper |
| 63 |
module, this holds a original module. |
| 64 |
this isn't used for resolving bindings, |
| 65 |
but used to avoid duplicating imports. */ |
| 66 |
ScmObj prefix; /* if symbol, all bindings in this module |
| 67 |
appear to have the prefix. used in an |
| 68 |
anonymous wrapper modules. */ |
| 69 |
ScmObj info; /* alist of metainfo; e.g. |
| 70 |
(source-info . <string>) */ |
| 71 |
int sealed; /* if true, no modification is allowed */ |
| 72 |
int placeholding; /* if true, this module is created just for |
| 73 |
hygienic identifiers, and the module body |
| 74 |
isn't loaded. */ |
| 75 |
}; |
| 76 |
|
| 77 |
#define SCM_MODULE(obj) ((ScmModule*)(obj)) |
| 78 |
#define SCM_MODULEP(obj) SCM_XTYPEP(obj, SCM_CLASS_MODULE) |
| 79 |
|
| 80 |
SCM_CLASS_DECL(Scm_ModuleClass); |
| 81 |
#define SCM_CLASS_MODULE (&Scm_ModuleClass) |
| 82 |
|
| 83 |
SCM_EXTERN ScmObj Scm_MakeModule(ScmSymbol *name, int error_if_exists); |
| 84 |
|
| 85 |
/* Flags for Scm_FindBinding (F), MakeBinding (M) |
| 86 |
and Scm_GlobalVariableRef (R)*/ |
| 87 |
enum { |
| 88 |
SCM_BINDING_STAY_IN_MODULE = (1L<<0), /*(F,R) do not search parent/imported*/ |
| 89 |
SCM_BINDING_CONST = (1L<<1), /*(M) constant binding */ |
| 90 |
SCM_BINDING_INLINABLE = (1L<<2), /*(M) inlinable binding */ |
| 91 |
SCM_BINDING_EXTERNAL = (1L<<3), /*(F) only search externally visible |
| 92 |
bindings, as if we're importing |
| 93 |
the module. Currently used to |
| 94 |
create alias binding. */ |
| 95 |
SCM_BINDING_DUMMY = (1L<<4) /*(M) inline or constant binding, |
| 96 |
inserted only during compilation. |
| 97 |
this is a dummy binding only to be |
| 98 |
recognized by the compiler to expand |
| 99 |
the following expressions. The |
| 100 |
binding is ot be overwritten by |
| 101 |
the proper one when the compiled |
| 102 |
module is loaded, so we won't warn |
| 103 |
the overwriting. */ |
| 104 |
}; |
| 105 |
|
| 106 |
SCM_EXTERN ScmGloc *Scm_FindBinding(ScmModule *module, ScmSymbol *symbol, |
| 107 |
int flags); |
| 108 |
SCM_EXTERN ScmGloc *Scm_MakeBinding(ScmModule *module, ScmSymbol *symbol, |
| 109 |
ScmObj value, int flags); |
| 110 |
SCM_EXTERN ScmObj Scm_GlobalVariableRef(ScmModule *module, |
| 111 |
ScmSymbol *symbol, |
| 112 |
int flags); |
| 113 |
SCM_EXTERN void Scm_HideBinding(ScmModule *module, ScmSymbol *symbol); |
| 114 |
SCM_EXTERN int Scm_AliasBinding(ScmModule *target, ScmSymbol *targetName, |
| 115 |
ScmModule *origin, ScmSymbol *originName); |
| 116 |
|
| 117 |
/* Convenience API. Wrapper of Scm_MakeBinding. */ |
| 118 |
SCM_EXTERN ScmObj Scm_Define(ScmModule *module, |
| 119 |
ScmSymbol *symbol, |
| 120 |
ScmObj value); |
| 121 |
SCM_EXTERN ScmObj Scm_DefineConst(ScmModule *module, |
| 122 |
ScmSymbol *symbol, |
| 123 |
ScmObj value); |
| 124 |
|
| 125 |
|
| 126 |
SCM_EXTERN ScmObj Scm_ExtendModule(ScmModule *module, ScmObj supers); |
| 127 |
SCM_EXTERN ScmObj Scm_ImportModule(ScmModule *module, ScmObj imported, |
| 128 |
ScmObj prefix, u_long flags); |
| 129 |
SCM_EXTERN ScmObj Scm_ImportModules(ScmModule *module, ScmObj list);/*deprecated*/ |
| 130 |
SCM_EXTERN ScmObj Scm_ExportSymbols(ScmModule *module, ScmObj list); |
| 131 |
SCM_EXTERN ScmObj Scm_ExportAll(ScmModule *module); |
| 132 |
SCM_EXTERN ScmModule *Scm_FindModule(ScmSymbol *name, int flags); |
| 133 |
SCM_EXTERN ScmObj Scm_AllModules(void); |
| 134 |
SCM_EXTERN void Scm_SelectModule(ScmModule *mod); |
| 135 |
SCM_EXTERN ScmObj Scm_ModuleExports(ScmModule *mod); |
| 136 |
|
| 137 |
SCM_EXTERN void Scm_ModuleSeal(ScmModule *mod); |
| 138 |
|
| 139 |
/* Flags for Scm_FindModule |
| 140 |
NB: Scm_FindModule's second arg has been changed since 0.8.6; |
| 141 |
before, it was just a boolean value to indicate whether a new |
| 142 |
module should be created (TRUE) or not (FALSE). We added a |
| 143 |
new flag value to make Scm_FindModule raises an error if the named |
| 144 |
module doesn't exist. This change should be transparent as far |
| 145 |
as the caller's using Gauche's definition of TRUE. */ |
| 146 |
enum { |
| 147 |
SCM_FIND_MODULE_CREATE = (1L<<0), /* Create if there's no named module */ |
| 148 |
SCM_FIND_MODULE_QUIET = (1L<<1), /* Do not signal an error if there's no |
| 149 |
named module, but return NULL |
| 150 |
instead. */ |
| 151 |
SCM_FIND_MODULE_PLACEHOLDING = (1L<<2) /* If module is newly created, |
| 152 |
mark it as placeholding. */ |
| 153 |
}; |
| 154 |
|
| 155 |
#define SCM_FIND_MODULE(name, flags) \ |
| 156 |
Scm_FindModule(SCM_SYMBOL(SCM_INTERN(name)), flags) |
| 157 |
|
| 158 |
SCM_EXTERN ScmObj Scm_ModuleNameToPath(ScmSymbol *name); |
| 159 |
SCM_EXTERN ScmObj Scm_PathToModuleName(ScmString *path); |
| 160 |
|
| 161 |
SCM_EXTERN ScmModule *Scm_NullModule(void); |
| 162 |
SCM_EXTERN ScmModule *Scm_SchemeModule(void); |
| 163 |
SCM_EXTERN ScmModule *Scm_GaucheModule(void); |
| 164 |
SCM_EXTERN ScmModule *Scm_UserModule(void); |
| 165 |
SCM_EXTERN ScmModule *Scm_CurrentModule(void); |
| 166 |
|
| 167 |
#define SCM_DEFINE(module, cstr, val) \ |
| 168 |
Scm_Define(SCM_MODULE(module), \ |
| 169 |
SCM_SYMBOL(SCM_INTERN(cstr)), \ |
| 170 |
SCM_OBJ(val)) |
| 171 |
|
| 172 |
/* OBSOLETED */ |
| 173 |
#define Scm_SymbolValue(m, s) Scm_GlobalVariableRef(m, s, FALSE) |
| 174 |
/* OBSOLETED */ |
| 175 |
#define SCM_SYMBOL_VALUE(module_name, symbol_name) \ |
| 176 |
Scm_SymbolValue(SCM_FIND_MODULE(module_name, 0), \ |
| 177 |
SCM_SYMBOL(SCM_INTERN(symbol_name))) |
| 178 |
|
| 179 |
|
| 180 |
#endif /*GAUCHE_MODULE_H*/ |