Debian Codegraph @

src/c/symbol.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 * symbol.d - symbols
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 <ecl/ecl.h>
16 #include <ecl/ecl-inl.h>
17 #include <ecl/internal.h>
18 #include <string.h>
19
20 cl_object
21 ecl_symbol_package(cl_object s)
22 {
23 if (Null(s))
24 return ECL_NIL_SYMBOL->symbol.hpack;
25 if (ecl_t_of(s) == t_symbol)
26 return s->symbol.hpack;
27 FEwrong_type_only_arg(@[symbol-package], s, @[symbol]);
28 }
29
30 int
31 ecl_symbol_type(cl_object s)
32 {
33 if (Null(s))
34 return ECL_NIL_SYMBOL->symbol.stype;
35 if (ecl_t_of(s) == t_symbol)
36 return s->symbol.stype;
37 FEwrong_type_nth_arg(@[symbol-name], 1, s, @[symbol]);
38 }
39
40 void
41 ecl_symbol_type_set(cl_object s, int type)
42 {
43 if (Null(s)) {
44 ECL_NIL_SYMBOL->symbol.stype = type;
45 return;
46 }
47 if (ecl_t_of(s) == t_symbol) {
48 s->symbol.stype = type;
49 return;
50 }
51 FEwrong_type_nth_arg(@[symbol-name], 1, s, @[symbol]);
52 }
53
54 cl_object
55 ecl_symbol_name(cl_object s)
56 {
57 if (Null(s)) {
58 return ECL_NIL_SYMBOL->symbol.name;
59 }
60 if (ecl_t_of(s) == t_symbol) {
61 return s->symbol.name;
62 }
63 FEwrong_type_only_arg(@[symbol-name], s, @[symbol]);
64 }
65
66 static cl_object *
67 ecl_symbol_plist(cl_object s)
68 {
69 if (Null(s)) {
70 return &ECL_NIL_SYMBOL->symbol.plist;
71 }
72 if (ecl_t_of(s) == t_symbol) {
73 return &s->symbol.plist;
74 }
75 FEwrong_type_only_arg(@[symbol-plist], s, @[symbol]);
76 }
77
78 /**********************************************************************/
79
80 static void FEtype_error_plist(cl_object x) /*__attribute__((noreturn))*/;
81
82 cl_object
83 cl_make_symbol(cl_object str)
84 {
85 cl_object x;
86 /* INV: In several places it is assumed that we copy the string! */
87 switch (ecl_t_of(str)) {
88 #ifdef ECL_UNICODE
89 case t_string:
90 if (!ecl_fits_in_base_string(str)) {
91 str = cl_copy_seq(str);
92 } else {
93 str = si_copy_to_simple_base_string(str);
94 }
95 break;
96 #endif
97 case t_base_string:
98 str = si_copy_to_simple_base_string(str);
99 break;
100 default:
101 FEwrong_type_only_arg(@[make-symbol],str,@[string]);
102 }
103 x = ecl_alloc_object(t_symbol);
104 x->symbol.name = str;
105 x->symbol.cname = ECL_NIL;
106 #ifdef ECL_THREADS
107 x->symbol.binding = ECL_MISSING_SPECIAL_BINDING;
108 #endif
109 ECL_SET(x,OBJNULL);
110 ECL_FMAKUNBOUND(x);
111 x->symbol.undef_entry = ecl_undefined_function_entry;
112 x->symbol.macfun = ECL_NIL;
113 x->symbol.sfdef = ECL_NIL;
114 x->symbol.plist = ECL_NIL;
115 x->symbol.hpack = ECL_NIL;
116 x->symbol.stype = ecl_stp_ordinary;
117 ecl_set_finalizer_unprotected(x, ECL_T);
118 @(return x);
119 }
120
121 /*
122 ecl_make_keyword(s) makes a keyword from C string s.
123 */
124 cl_object
125 ecl_make_keyword(const char *s)
126 {
127 cl_object x = _ecl_intern(s, cl_core.keyword_package);
128 /* cl_export(x, keyword_package); this is implicit in ecl_intern() */
129 return x;
130 }
131
132 cl_object
133 ecl_make_symbol(const char *s, const char *p)
134 {
135 ecl_def_ct_base_string(pack_name,p,strlen(p),,);
136 cl_object x = _ecl_intern(s, pack_name);
137 /* cl_export(x, keyword_package); this is implicit in ecl_intern() */
138 return x;
139 }
140
141 cl_object
142 ecl_symbol_value(cl_object s)
143 {
144 if (ecl_unlikely(Null(s))) {
145 return s;
146 }
147 if (ecl_unlikely(ecl_t_of(s) != t_symbol)) {
148 FEwrong_type_nth_arg(@[symbol-value], 1, s, @[symbol]);
149 }
150 return ecl_cmp_symbol_value(ecl_process_env(), s);
151 }
152
153 /* ecl_cmp_symbol_value does the minimal amount of checking necessary
154 * to implement SYMBOL-VALUE for objects that have been checked to be
155 * non-null symbols by the compiler. */
156 cl_object
157 ecl_cmp_symbol_value(cl_env_ptr the_env, cl_object s)
158 {
159 #ifndef ECL_FINAL
160 /* Symbols are not initialized yet. This test is issued only during ECL
161 compilation to ensure, that we have no early references in the core. */
162 if(cl_num_symbols_in_core < 3) {
163 ecl_internal_error("SYMBOL-VALUE: symbols are not initialized yet.");
164 }
165 #endif
166 /* FIXME: Should we check symbol type? */
167 cl_object value = ECL_SYM_VAL(the_env, s);
168 if (ecl_unlikely(value == OBJNULL)) {
169 FEunbound_variable(s);
170 }
171 return value;
172 }
173
174 static void
175 FEtype_error_plist(cl_object x)
176 {
177 cl_error(9, @'simple-type-error', @':format-control',
178 @"Not a valid property list ~D",
179 @':format-arguments', cl_list(1, x),
180 @':expected-type', @'si::property-list',
181 @':datum', x);
182 }
183
184 cl_object
185 ecl_getf(cl_object place, cl_object indicator, cl_object deflt)
186 {
187 cl_object l;
188
189 for (l = place; CONSP(l); ) {
190 cl_object cdr_l = ECL_CONS_CDR(l);
191 if (!CONSP(cdr_l))
192 break;
193 if (ECL_CONS_CAR(l) == indicator)
194 return ECL_CONS_CAR(cdr_l);
195 l = ECL_CONS_CDR(cdr_l);
196 }
197 if (l != ECL_NIL)
198 FEtype_error_plist(place);
199 return(deflt);
200 }
201
202 cl_object
203 ecl_get(cl_object s, cl_object p, cl_object d)
204 {
205 return ecl_getf(*ecl_symbol_plist(s), p, d);
206 }
207
208 /*
209 (SI:PUT-F plist value indicator)
210 returns the new property list with value for property indicator.
211 It will be used in SETF for GETF.
212 */
213 cl_object
214 si_put_f(cl_object place, cl_object value, cl_object indicator)
215 {
216 cl_object l;
217
218 /* This loop guarantees finishing for circular lists */
219 for (l = place; CONSP(l); ) {
220 cl_object cdr_l = ECL_CONS_CDR(l);
221 if (!CONSP(cdr_l))
222 break;
223 if (ECL_CONS_CAR(l) == indicator) {
224 ECL_RPLACA(cdr_l, value);
225 @(return place);
226 }
227 l = ECL_CONS_CDR(cdr_l);
228 }
229 if (l != ECL_NIL)
230 FEtype_error_plist(place);
231 place = CONS(value, place);
232 @(return CONS(indicator, place));
233 }
234
235 /*
236 Remf(p, i) removes property i
237 from the property list pointed by p,
238 which is a pointer to an cl_object.
239 The returned value of remf(p, i) is:
240
241 TRUE if the property existed
242 FALSE otherwise.
243 */
244 static bool
245 remf(cl_object *place, cl_object indicator)
246 {
247 cl_object l = *place, tail = ECL_NIL;
248 while (!Null(l)) {
249 cl_object ind;
250 if (!LISTP(l))
251 FEtype_error_plist(*place);
252 ind = ECL_CONS_CAR(l);
253 l = ECL_CONS_CDR(l);
254 if (!CONSP(l))
255 FEtype_error_plist(*place);
256 if (ind == indicator) {
257 l = ECL_CONS_CDR(l);
258 if (Null(tail))
259 *place = l;
260 else
261 ECL_RPLACD(tail, l);
262 return TRUE;
263 }
264 tail = l;
265 l = ECL_CONS_CDR(l);
266 }
267 return FALSE;
268 }
269
270 bool
271 ecl_keywordp(cl_object s)
272 {
273 return (ecl_t_of(s) == t_symbol) && (s->symbol.hpack == cl_core.keyword_package);
274 }
275
276 @(defun get (sym indicator &optional deflt)
277 cl_object *plist;
278 @
279 plist = ecl_symbol_plist(sym);
280 @(return ecl_getf(*plist, indicator, deflt));
281 @)
282
283 cl_object
284 cl_remprop(cl_object sym, cl_object prop)
285 {
286 cl_object *plist = ecl_symbol_plist(sym);
287 @(return (remf(plist, prop)? ECL_T: ECL_NIL));
288 }
289
290 cl_object
291 cl_symbol_plist(cl_object sym)
292 {
293 @(return *ecl_symbol_plist(sym));
294 }
295
296 @(defun getf (place indicator &optional deflt)
297 @
298 @(return ecl_getf(place, indicator, deflt));
299 @)
300
301 cl_object
302 cl_get_properties(cl_object place, cl_object indicator_list)
303 {
304 const cl_env_ptr the_env = ecl_process_env();
305 cl_object l;
306
307 for (l = place; CONSP(l); ) {
308 cl_object cdr_l = ECL_CONS_CDR(l);
309 if (!CONSP(cdr_l))
310 break;
311 if (ecl_member_eq(ECL_CONS_CAR(l), indicator_list))
312 ecl_return3(the_env,ECL_CONS_CAR(l),ECL_CONS_CAR(cdr_l),l);
313 l = ECL_CONS_CDR(cdr_l);
314 }
315 if (l != ECL_NIL)
316 FEtype_error_plist(place);
317 ecl_return3(the_env, ECL_NIL, ECL_NIL, ECL_NIL);
318 }
319
320 cl_object
321 cl_symbol_name(cl_object x)
322 {
323 ecl_return1(ecl_process_env(), ecl_symbol_name(x));
324 }
325
326 @(defun copy_symbol (sym &optional cp &aux x)
327 @
328 if (Null(sym)) {
329 sym = ECL_NIL_SYMBOL;
330 }
331 x = cl_make_symbol(ecl_symbol_name(sym));
332 if (!Null(cp)) {
333 x->symbol.stype = sym->symbol.stype;
334 x->symbol.value = sym->symbol.value;
335 x->symbol.plist = cl_copy_list(sym->symbol.plist);
336 x->symbol.undef_entry = sym->symbol.undef_entry;
337 x->symbol.sfdef = sym->symbol.sfdef;
338 if (ECL_FBOUNDP(sym)) {
339 x->symbol.gfdef = sym->symbol.gfdef;
340 } else {
341 ECL_FMAKUNBOUND(x);
342 }
343 x->symbol.macfun = sym->symbol.macfun;
344 #ifdef ECL_THREADS
345 x->symbol.binding = ECL_MISSING_SPECIAL_BINDING;
346 #endif
347 si_copy_sysprop(sym, x);
348 }
349 @(return x);
350 @)
351
352 @(defun gensym (&optional (prefix cl_core.gensym_prefix))
353 cl_object counter, output;
354 bool increment;
355 @ {
356 if (ecl_stringp(prefix)) {
357 counter = ECL_SYM_VAL(the_env, @'*gensym-counter*');
358 increment = 1;
359 } else if (ecl_t_of(prefix) == t_fixnum || ecl_t_of(prefix) == t_bignum) {
360 counter = prefix;
361 prefix = cl_core.gensym_prefix;
362 increment = 0;
363 } else {
364 FEwrong_type_nth_arg(@[gensym],2,prefix,
365 cl_list(3, @'or', @'string', @'integer'));
366 }
367 assert_type_non_negative_integer(counter);
368 output = ecl_make_string_output_stream(64, 1);
369 ecl_bds_bind(the_env, @'*print-escape*', ECL_NIL);
370 ecl_bds_bind(the_env, @'*print-readably*', ECL_NIL);
371 ecl_bds_bind(the_env, @'*print-base*', ecl_make_fixnum(10));
372 ecl_bds_bind(the_env, @'*print-radix*', ECL_NIL);
373 si_write_ugly_object(prefix, output);
374 si_write_ugly_object(counter, output);
375 ecl_bds_unwind_n(the_env, 4);
376 output = cl_make_symbol(cl_get_output_stream_string(output));
377 if (increment)
378 ECL_SETQ(the_env, @'*gensym-counter*',ecl_one_plus(counter));
379 @(return output);
380 } @)
381
382 @(defun gentemp (&optional (prefix cl_core.gentemp_prefix) (pack ecl_current_package()))
383 cl_object output, s;
384 int intern_flag;
385 @
386 unlikely_if (!ECL_STRINGP(prefix))
387 FEwrong_type_nth_arg(@[gentemp], 1, prefix, @[string]);
388 pack = si_coerce_to_package(pack);
389 ONCE_MORE:
390 output = ecl_make_string_output_stream(64, 1);
391 ecl_bds_bind(the_env, @'*print-escape*', ECL_NIL);
392 ecl_bds_bind(the_env, @'*print-readably*', ECL_NIL);
393 ecl_bds_bind(the_env, @'*print-base*', ecl_make_fixnum(10));
394 ecl_bds_bind(the_env, @'*print-radix*', ECL_NIL);
395 si_write_ugly_object(prefix, output);
396 si_write_ugly_object(cl_core.gentemp_counter, output);
397 ecl_bds_unwind_n(the_env, 4);
398 cl_core.gentemp_counter = ecl_one_plus(cl_core.gentemp_counter);
399 s = ecl_intern(cl_get_output_stream_string(output), pack, &intern_flag);
400 if (intern_flag != 0)
401 goto ONCE_MORE;
402 @(return s);
403 @)
404
405 cl_object
406 cl_symbol_package(cl_object sym)
407 {
408 @(return ecl_symbol_package(sym));
409 }
410
411 cl_object
412 cl_keywordp(cl_object sym)
413 {
414 @(return (ecl_keywordp(sym)? ECL_T: ECL_NIL));
415 }
416
417 /*
418 (SI:REM-F plist indicator) returns two values:
419
420 * the new property list
421 in which property indcator is removed
422
423 * T if really removed
424 NIL otherwise.
425
426 It will be used for macro REMF.
427 */
428 cl_object
429 si_rem_f(cl_object plist, cl_object indicator)
430 {
431 cl_env_ptr the_env = ecl_process_env();
432 bool found = remf(&plist, indicator);
433 ecl_return2(the_env, plist, (found? ECL_T : ECL_NIL));
434 }
435
436 cl_object
437 si_set_symbol_plist(cl_object sym, cl_object plist)
438 {
439 *ecl_symbol_plist(sym) = plist;
440 @(return plist);
441 }
442
443 #ifdef ECL_THREADS
444 cl_object
445 mp_compare_and_swap_symbol_plist(cl_object x, cl_object old, cl_object new)
446 {
447 return ecl_compare_and_swap(ecl_symbol_plist(x), old, new);
448 }
449 #endif /* ECL_THREADS */
450
451 cl_object
452 si_putprop(cl_object sym, cl_object value, cl_object indicator)
453 {
454 cl_object *plist = ecl_symbol_plist(sym);
455 *plist = si_put_f(*plist, value, indicator);
456 @(return value);
457 }
458
459 /* Added for defstruct. Beppe */
460 @(defun si::put-properties (sym &rest ind_values)
461 @
462 while (--narg >= 2) {
463 cl_object prop = ecl_va_arg(ind_values);
464 si_putprop(sym, ecl_va_arg(ind_values), prop);
465 narg--;
466 }
467 @(return sym);
468 @)
469
470 cl_object
471 @si::*make-special(cl_object sym)
472 {
473 int type = ecl_symbol_type(sym);
474 if (type & ecl_stp_constant)
475 FEerror("~S is a constant.", 1, sym);
476 ecl_symbol_type_set(sym, type | ecl_stp_special);
477 cl_remprop(sym, @'si::symbol-macro');
478 @(return sym);
479 }
480
481 cl_object
482 @si::*make-constant(cl_object sym, cl_object val)
483 {
484 int type = ecl_symbol_type(sym);
485 if (type & ecl_stp_special)
486 FEerror("The argument ~S to DEFCONSTANT is a special variable.",
487 1, sym);
488 ecl_symbol_type_set(sym, type | ecl_stp_constant);
489 ECL_SET(sym, val);
490 @(return sym);
491 }
492
493 void
494 ecl_defvar(cl_object sym, cl_object val)
495 {
496 si_safe_eval(3, cl_list(3, @'defvar', sym, cl_list(2, @'quote', val)), ECL_NIL, ECL_NIL);
497 }
498
499 void
500 ecl_defparameter(cl_object sym, cl_object val)
501 {
502 si_safe_eval(3, cl_list(3, @'defparameter', sym, cl_list(2, @'quote', val)), ECL_NIL, ECL_NIL);
503 }