| 1 |
/* |
| 2 |
* gloc.c - gloc implementation |
| 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 |
#define LIBGAUCHE_BODY |
| 35 |
#include "gauche.h" |
| 36 |
#include "gauche/priv/configP.h" |
| 37 |
#include "gauche/priv/glocP.h" |
| 38 |
|
| 39 |
/*--------------------------------------------------------------- |
| 40 |
* GLOCs |
| 41 |
*/ |
| 42 |
|
| 43 |
static void gloc_print(ScmObj obj, ScmPort *port, |
| 44 |
ScmWriteContext *ctx SCM_UNUSED) |
| 45 |
{ |
| 46 |
ScmGloc *g = SCM_GLOC(obj); |
| 47 |
Scm_Printf(port, "#<gloc %S#%S%s>", g->module->name, |
| 48 |
g->name, |
| 49 |
(Scm_GlocConstP(g) |
| 50 |
? " const" |
| 51 |
: (Scm_GlocInlinableP(g) |
| 52 |
? " inlinable" |
| 53 |
: (SCM_GLOC_PHANTOM_BINDING_P(g) |
| 54 |
? " phantom" |
| 55 |
: "")))); |
| 56 |
} |
| 57 |
|
| 58 |
SCM_DEFINE_BUILTIN_CLASS_SIMPLE(Scm_GlocClass, gloc_print); |
| 59 |
|
| 60 |
ScmObj Scm_MakeGloc(ScmSymbol *sym, ScmModule *module) |
| 61 |
{ |
| 62 |
ScmGloc *g = SCM_NEW(ScmGloc); |
| 63 |
SCM_SET_CLASS(g, &Scm_GlocClass); |
| 64 |
g->name = sym; |
| 65 |
g->module = module; |
| 66 |
g->value = SCM_UNBOUND; |
| 67 |
g->hidden = FALSE; |
| 68 |
g->getter = NULL; |
| 69 |
g->setter = NULL; |
| 70 |
return SCM_OBJ(g); |
| 71 |
} |
| 72 |
|
| 73 |
/* Public accessor/mutators |
| 74 |
We don't allow Scm_GlocGetValues to return SCM_UNBOUND or SCM_UNINITIALIZED. |
| 75 |
(UNINITIALIZED only occurs temporarily during compilation, but we check |
| 76 |
it just in case.) |
| 77 |
*/ |
| 78 |
ScmObj Scm_GlocGetValue(ScmGloc *gloc) |
| 79 |
{ |
| 80 |
ScmObj v = SCM_GLOC_GET(gloc); |
| 81 |
if (SCM_UNBOUNDP(v)) { |
| 82 |
Scm_VMThrowException(Scm_VM(), |
| 83 |
Scm_MakeUnboundVariableError(SCM_OBJ(gloc->name)), |
| 84 |
0); |
| 85 |
} |
| 86 |
if (SCM_UNINITIALIZEDP(v)) { |
| 87 |
Scm_Error("uninitialized variable: %S", SCM_OBJ(gloc->name)); |
| 88 |
} |
| 89 |
return v; |
| 90 |
} |
| 91 |
|
| 92 |
void Scm_GlocSetValue(ScmGloc *gloc, ScmObj val) |
| 93 |
{ |
| 94 |
SCM_GLOC_SET(gloc, val); |
| 95 |
} |
| 96 |
|
| 97 |
int Scm_GlocPhantomBindingP(ScmGloc *gloc) |
| 98 |
{ |
| 99 |
return SCM_GLOC_PHANTOM_BINDING_P(gloc); |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
/* special setters for const and inlinable bindings. */ |
| 104 |
ScmObj Scm_GlocConstSetter(ScmGloc *gloc, ScmObj val SCM_UNUSED) |
| 105 |
{ |
| 106 |
Scm_Error("cannot change constant value of %S#%S", |
| 107 |
gloc->module->name, gloc->name); |
| 108 |
return SCM_UNDEFINED; /* dummy */ |
| 109 |
} |
| 110 |
|
| 111 |
ScmObj Scm_GlocInlinableSetter(ScmGloc *gloc, ScmObj val) |
| 112 |
{ |
| 113 |
Scm_Warn("altering inlinable binding: %S#%S", |
| 114 |
gloc->module->name, gloc->name); |
| 115 |
return val; |
| 116 |
} |
| 117 |
|
| 118 |
/* Kludge - we need to distinguish 'dummy' inlinable bindings (which are |
| 119 |
only needed during compilation in order to inline expand in the same |
| 120 |
compilation unit), and real inlinable bindings. The difference is that |
| 121 |
the latter can supersede the former. We may do it with flags. For now, |
| 122 |
we keep the strategy of setter pointer comparison. |
| 123 |
*/ |
| 124 |
static ScmObj gloc_dummy_inlinable_setter(ScmGloc *gloc, ScmObj val) |
| 125 |
{ |
| 126 |
Scm_Warn("altering inlinable binding: %S#%S", |
| 127 |
gloc->module->name, gloc->name); |
| 128 |
return val; |
| 129 |
} |
| 130 |
|
| 131 |
int Scm_GlocConstP(ScmGloc *gloc) |
| 132 |
{ |
| 133 |
return ((gloc)->setter == Scm_GlocConstSetter); |
| 134 |
} |
| 135 |
|
| 136 |
int Scm_GlocInlinableP(ScmGloc *gloc) |
| 137 |
{ |
| 138 |
return (((gloc)->setter == Scm_GlocInlinableSetter) |
| 139 |
|| ((gloc)->setter == gloc_dummy_inlinable_setter)); |
| 140 |
} |
| 141 |
|
| 142 |
static int gloc_dummy_inlinable_p(ScmGloc *gloc) |
| 143 |
{ |
| 144 |
return ((gloc)->setter == gloc_dummy_inlinable_setter); |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
/* This is to check if the module can supesede the exsiting binding of gloc |
| 149 |
* with the new value. (It is different from setting the new value, which |
| 150 |
* would be handled by SCM_GLOC_SET. We're talking the case that a new gloc |
| 151 |
* is created that will replace the current gloc, by Scm_MakeBinding.) |
| 152 |
* |
| 153 |
* If previous gloc is constant or inlinable, it is only replacable with the |
| 154 |
* same kind and value. Except that if the previous gloc is dummy inlinable, |
| 155 |
* and the new gloc is inlinable; or, the previous value and new value are |
| 156 |
* both classes (which means it is class redefinition). |
| 157 |
*/ |
| 158 |
int Scm_GlocSupersedableP(ScmGloc *gloc, u_long flags, ScmObj newval) |
| 159 |
{ |
| 160 |
if (Scm_GlocConstP(gloc)) { |
| 161 |
return ((flags & SCM_BINDING_CONST) && Scm_EqualP(gloc->value, newval)); |
| 162 |
} |
| 163 |
if (Scm_GlocInlinableP(gloc)) { |
| 164 |
if (gloc_dummy_inlinable_p(gloc)) { |
| 165 |
return (flags & SCM_BINDING_INLINABLE); |
| 166 |
} else if (flags & SCM_BINDING_INLINABLE) { |
| 167 |
return (Scm_EqualP(gloc->value, newval) |
| 168 |
|| (SCM_ISA(gloc->value, SCM_CLASS_CLASS) |
| 169 |
&& SCM_ISA(newval, SCM_CLASS_CLASS))); |
| 170 |
} else { |
| 171 |
return FALSE; |
| 172 |
} |
| 173 |
} |
| 174 |
return TRUE; |
| 175 |
} |
| 176 |
|
| 177 |
/* Change binding flags. Do not use casually. */ |
| 178 |
void Scm_GlocMark(ScmGloc *gloc, int flags) |
| 179 |
{ |
| 180 |
if (flags & SCM_BINDING_CONST) { |
| 181 |
gloc->setter = Scm_GlocConstSetter; |
| 182 |
} else if (flags & SCM_BINDING_INLINABLE) { |
| 183 |
if (flags & SCM_BINDING_DUMMY) { |
| 184 |
gloc->setter = gloc_dummy_inlinable_setter; |
| 185 |
} else { |
| 186 |
gloc->setter = Scm_GlocInlinableSetter; |
| 187 |
} |
| 188 |
} else { |
| 189 |
gloc->setter = NULL; |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
/* For the backward ABI compatibility */ |
| 194 |
ScmObj Scm_GlocMarkConst(ScmGloc *gloc) |
| 195 |
{ |
| 196 |
Scm_GlocMark(gloc, SCM_BINDING_CONST); |
| 197 |
return SCM_OBJ(gloc); |
| 198 |
} |
| 199 |
|
| 200 |
/* For the backward ABI compatibility */ |
| 201 |
ScmObj Scm_GlocUnmarkConst(ScmGloc *gloc) |
| 202 |
{ |
| 203 |
Scm_GlocMark(gloc, 0); |
| 204 |
return SCM_OBJ(gloc); |
| 205 |
} |