Debian Codegraph @

src/c/assignment.d

raw
1 /* -*- Mode: C; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2 /* vim: set filetype=c tabstop=2 shiftwidth=2 expandtab: */
3
4 /*
5 * assignment.c - assignment
6 *
7 * Copyright (c) 1984, Taiichi Yuasa and Masami Hagiya.
8 * Copyright (c) 1990, Giuseppe Attardi.
9 * Copyright (c) 2001, Juan Jose Garcia Ripoll.
10 *
11 * See file 'LICENSE' for the copyright details.
12 *
13 */
14
15 #include <string.h>
16 #include <ecl/ecl.h>
17 #include <ecl/internal.h>
18
19 static void FEconstant_assignment(cl_object var) ecl_attr_noreturn;
20
21 static void
22 FEconstant_assignment(cl_object var)
23 {
24 FEinvalid_variable("Cannot assign to the constant ~S.", var);
25 }
26
27 cl_object
28 cl_set(cl_object var, cl_object value)
29 {
30 const cl_env_ptr env = ecl_process_env();
31 unlikely_if (Null(var)) {
32 FEconstant_assignment(var);
33 }
34 unlikely_if (ecl_t_of(var) != t_symbol) {
35 FEwrong_type_nth_arg(@[set], 1, var, @[symbol]);
36 }
37 ecl_return1(env, ecl_cmp_setq(env, var, value));
38 }
39
40 cl_object
41 ecl_setq(cl_env_ptr env, cl_object var, cl_object value)
42 {
43 unlikely_if (Null(var)) {
44 FEconstant_assignment(var);
45 }
46 unlikely_if (ecl_t_of(var) != t_symbol) {
47 FEwrong_type_nth_arg(@[setq], 1, var, @[symbol]);
48 }
49 return ecl_cmp_setq(env, var, value);
50 }
51
52 /* ecl_cmp_setq does the minimal amount of checking necessary to
53 * implement SETQ for objects that have been checked to be non-null
54 * symbols by the compiler. */
55 cl_object
56 ecl_cmp_setq(cl_env_ptr env, cl_object var, cl_object value)
57 {
58 unlikely_if (var->symbol.stype & ecl_stp_constant) {
59 FEconstant_assignment(var);
60 }
61 return ECL_SETQ(env, var, value);
62 }
63
64 #ifdef ECL_THREADS
65 cl_object
66 mp_compare_and_swap_symbol_value(cl_object var, cl_object old, cl_object new)
67 {
68 unlikely_if (Null(var)) {
69 FEconstant_assignment(var);
70 }
71 unlikely_if (ecl_t_of(var) != t_symbol) {
72 FEwrong_type_nth_arg(@[mp::compare-and-swap-symbol-value], 1, var, @[symbol]);
73 }
74 unlikely_if (var->symbol.stype & ecl_stp_constant) {
75 FEconstant_assignment(var);
76 }
77 return ecl_compare_and_swap(ecl_bds_ref(ecl_process_env(), var), old, new);
78 }
79
80 cl_object
81 mp_atomic_incf_symbol_value(cl_object var, cl_object increment)
82 {
83 unlikely_if (Null(var)) {
84 FEconstant_assignment(var);
85 }
86 unlikely_if (ecl_t_of(var) != t_symbol) {
87 FEwrong_type_nth_arg(@[mp::atomic-incf-symbol-value], 1, var, @[symbol]);
88 }
89 unlikely_if (var->symbol.stype & ecl_stp_constant) {
90 FEconstant_assignment(var);
91 }
92 return ecl_atomic_incf(ecl_bds_ref(ecl_process_env(), var), increment);
93 }
94 #endif /* ECL_THREADS */
95
96 static cl_object
97 unbound_setf_function_error(cl_narg narg, ...)
98 {
99 const cl_env_ptr the_env = ecl_process_env();
100 cl_object name = the_env->function->cclosure.env;
101 FEundefined_function(cl_list(2, @'setf', name));
102 }
103
104 static cl_object
105 make_setf_function_error(cl_object name)
106 {
107 return ecl_make_cclosure_va((cl_objectfn)unbound_setf_function_error,
108 name, ECL_NIL, 0);
109 }
110
111 cl_object
112 ecl_setf_definition(cl_object sym, cl_object createp)
113 {
114 cl_env_ptr the_env = ecl_process_env();
115 cl_object pair = sym->symbol.sfdef;
116 if (Null(pair) && !Null(createp)) {
117 createp = make_setf_function_error(sym);
118 pair = ecl_cons(createp, ECL_NIL);
119 sym->symbol.sfdef = pair;
120 }
121 return pair;
122 }
123
124 cl_object
125 si_setf_definition(cl_object sym, cl_object value)
126 {
127 @(return ecl_setf_definition(sym, value));
128 }
129
130 static void
131 ecl_rem_setf_definition(cl_object sym)
132 {
133 cl_env_ptr the_env = ecl_process_env();
134 cl_object pair = sym->symbol.sfdef;
135 if (!Null(pair)) {
136 ECL_RPLACA(pair, make_setf_function_error(sym));
137 ECL_RPLACD(pair, ECL_NIL);
138 }
139 }
140
141 @(defun si::fset (fname def &optional macro pprint)
142 cl_object sym = si_function_block_name(fname);
143 cl_object pack;
144 bool mflag;
145 int type;
146 @
147 if (Null(cl_functionp(def)))
148 FEinvalid_function(def);
149 pack = ecl_symbol_package(sym);
150 if (pack != ECL_NIL
151 && pack->pack.locked
152 && ECL_SYM_VAL(ecl_process_env(),
153 @'si::*ignore-package-locks*') == ECL_NIL) {
154 CEpackage_error("Attempt to redefine function ~S in locked package.",
155 "Ignore lock and proceed", pack, 1, fname);
156 }
157 mflag = !Null(macro);
158 type = ecl_symbol_type(sym);
159 if ((type & ecl_stp_special_form) && !mflag) {
160 FEerror("Given that ~S is a special form, ~S cannot be defined as a function.",
161 2, sym, fname);
162 }
163 if (ECL_SYMBOLP(fname)) {
164 if (mflag) {
165 type |= ecl_stp_macro;
166 sym->symbol.macfun = def;
167 ECL_FMAKUNBOUND(sym);
168 } else {
169 type &= ~ecl_stp_macro;
170 ECL_SYM_FUN(sym) = def;
171 }
172 ecl_symbol_type_set(sym, type);
173 ecl_clear_compiler_properties(sym);
174 #ifndef ECL_CMU_FORMAT
175 if (pprint == ECL_NIL)
176 si_rem_sysprop(sym, @'si::pretty-print-format');
177 else
178 si_put_sysprop(sym, @'si::pretty-print-format', pprint);
179 #endif
180 } else if (mflag) {
181 FEerror("~S is not a valid name for a macro.", 1, fname);
182 } else {
183 cl_object pair = ecl_setf_definition(sym, def);
184 ECL_RPLACA(pair, def);
185 ECL_RPLACD(pair, sym);
186 }
187 @(return def);
188 @)
189
190 cl_object
191 cl_makunbound(cl_object sym)
192 {
193 if (ecl_symbol_type(sym) & ecl_stp_constant)
194 FEinvalid_variable("Cannot unbind the constant ~S.", sym);
195 ECL_SETQ(ecl_process_env(), sym, OBJNULL);
196 @(return sym);
197 }
198
199 cl_object
200 cl_fmakunbound(cl_object fname)
201 {
202 cl_object sym = si_function_block_name(fname);
203 cl_object pack = ecl_symbol_package(sym);
204 if (pack != ECL_NIL
205 && pack->pack.locked
206 && ECL_SYM_VAL(ecl_process_env(),
207 @'si::*ignore-package-locks*') == ECL_NIL) {
208 CEpackage_error("Attempt to redefine function ~S in locked package.",
209 "Ignore lock and proceed", pack, 1, fname);
210 }
211 if (ECL_SYMBOLP(fname)) {
212 ecl_clear_compiler_properties(sym);
213 ECL_FMAKUNBOUND(sym);
214 sym->symbol.macfun = ECL_NIL;
215 ecl_symbol_type_set(sym, ecl_symbol_type(sym) & ~ecl_stp_macro);
216 } else {
217 ecl_rem_setf_definition(sym);
218 si_rem_sysprop(sym, @'si::setf-method');
219 }
220 @(return fname);
221 }
222
223 void
224 ecl_clear_compiler_properties(cl_object sym)
225 {
226 if (ecl_option_values[ECL_OPT_BOOTED]) {
227 funcall(2, @'si::clear-compiler-properties', sym);
228 }
229 }
230
231 cl_object
232 si_get_sysprop(cl_object sym, cl_object prop)
233 {
234 cl_env_ptr the_env = ecl_process_env();
235 ECL_WITH_GLOBAL_ENV_RDLOCK_BEGIN(the_env) {
236 cl_object plist = ecl_gethash_safe(sym, cl_core.system_properties, ECL_NIL);
237 prop = ecl_getf(plist, prop, OBJNULL);
238 } ECL_WITH_GLOBAL_ENV_RDLOCK_END;
239 if (prop == OBJNULL) {
240 @(return ECL_NIL ECL_NIL);
241 } else {
242 @(return prop ECL_T);
243 }
244 }
245
246 cl_object
247 si_put_sysprop(cl_object sym, cl_object prop, cl_object value)
248 {
249 cl_env_ptr the_env = ecl_process_env();
250 ECL_WITH_GLOBAL_ENV_WRLOCK_BEGIN(the_env) {
251 cl_object plist = ecl_gethash_safe(sym, cl_core.system_properties, ECL_NIL);
252 ecl_sethash(sym, cl_core.system_properties, si_put_f(plist, value, prop));
253 } ECL_WITH_GLOBAL_ENV_WRLOCK_END;
254 @(return value);
255 }
256
257 cl_object
258 si_rem_sysprop(cl_object sym, cl_object prop)
259 {
260 const cl_env_ptr the_env = ecl_process_env();
261 cl_object plist, found;
262 ECL_WITH_GLOBAL_ENV_WRLOCK_BEGIN(the_env) {
263 plist = ecl_gethash_safe(sym, cl_core.system_properties, ECL_NIL);
264 plist = si_rem_f(plist, prop);
265 found = ecl_nth_value(the_env, 1);
266 ecl_sethash(sym, cl_core.system_properties, plist);
267 } ECL_WITH_GLOBAL_ENV_WRLOCK_END;
268 ecl_return1(the_env, found);
269 }
270
271 cl_object
272 si_copy_sysprop(cl_object sym_old, cl_object sym_new)
273 {
274 cl_env_ptr the_env = ecl_process_env();
275 cl_object plist = ECL_NIL;
276 ECL_WITH_GLOBAL_ENV_WRLOCK_BEGIN(the_env) {
277 plist = ecl_gethash_safe(sym_old, cl_core.system_properties, ECL_NIL);
278 if (!Null(plist)) {
279 ecl_sethash(sym_new, cl_core.system_properties, plist);
280 }
281 } ECL_WITH_GLOBAL_ENV_WRLOCK_END;
282 @(return plist);
283 }