| 1 |
/* -*- Mode: C; c-basic-offset: 2; indent-tabs-mode: nil -*- */ |
| 2 |
/* vim: set filetype=c tabstop=2 shiftwidth=2 expandtab: */ |
| 3 |
|
| 4 |
/* |
| 5 |
* structure.d - structure interface |
| 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 <string.h> |
| 18 |
|
| 19 |
/******************************* ------- ******************************/ |
| 20 |
|
| 21 |
static bool |
| 22 |
structure_subtypep(cl_object x, cl_object y) |
| 23 |
{ |
| 24 |
if (ECL_CLASS_NAME(x) == y) { |
| 25 |
return TRUE; |
| 26 |
} else { |
| 27 |
cl_object superiors = ECL_CLASS_SUPERIORS(x); |
| 28 |
loop_for_on_unsafe(superiors) { |
| 29 |
if (structure_subtypep(ECL_CONS_CAR(superiors), y)) |
| 30 |
return TRUE; |
| 31 |
} end_loop_for_on_unsafe(superiors); |
| 32 |
return FALSE; |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
cl_object |
| 37 |
si_structure_subtype_p(cl_object x, cl_object y) |
| 38 |
{ |
| 39 |
@(return ((ecl_t_of(x) == T_STRUCTURE |
| 40 |
&& structure_subtypep(ECL_STRUCT_TYPE(x), y)) ? ECL_T : ECL_NIL)); |
| 41 |
} |
| 42 |
|
| 43 |
@(defun si::make-structure (type &rest args) |
| 44 |
cl_object x; |
| 45 |
int i; |
| 46 |
@ |
| 47 |
x = ecl_alloc_object(T_STRUCTURE); |
| 48 |
ECL_STRUCT_TYPE(x) = type; |
| 49 |
ECL_STRUCT_SLOTS(x) = NULL; /* for GC sake */ |
| 50 |
ECL_STRUCT_LENGTH(x) = --narg; |
| 51 |
ECL_STRUCT_SLOTS(x) = (cl_object *)ecl_alloc_align(sizeof(cl_object)*narg, sizeof(cl_object)); |
| 52 |
x->instance.slotds = ECL_UNBOUND; |
| 53 |
x->instance.stamp = ECL_CLASS_OF(x)->instance.class_stamp; |
| 54 |
if (narg >= ECL_SLOTS_LIMIT) |
| 55 |
FEerror("Limit on structure size exceeded: ~S slots requested.", |
| 56 |
1, ecl_make_fixnum(narg)); |
| 57 |
for (i = 0; i < narg; i++) |
| 58 |
ECL_STRUCT_SLOT(x, i) = ecl_va_arg(args); |
| 59 |
@(return x); |
| 60 |
@) |
| 61 |
|
| 62 |
#define ecl_copy_structure si_copy_instance |
| 63 |
|
| 64 |
cl_object |
| 65 |
cl_copy_structure(cl_object s) |
| 66 |
{ |
| 67 |
switch (ecl_t_of(s)) { |
| 68 |
case t_instance: |
| 69 |
s = ecl_copy_structure(s); |
| 70 |
break; |
| 71 |
case t_list: |
| 72 |
#ifdef ECL_UNICODE |
| 73 |
case t_string: |
| 74 |
#endif |
| 75 |
case t_base_string: |
| 76 |
case t_bitvector: |
| 77 |
case t_vector: |
| 78 |
s = cl_copy_seq(s); |
| 79 |
break; |
| 80 |
default: |
| 81 |
FEwrong_type_only_arg(@[copy-structure], s, @[structure]); |
| 82 |
} |
| 83 |
@(return s); |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
/* Kept only for compatibility. One should use class-of or type-of. */ |
| 88 |
cl_object |
| 89 |
si_structure_name(cl_object s) |
| 90 |
{ |
| 91 |
if (ecl_unlikely(Null(si_structurep(s)))) |
| 92 |
FEwrong_type_only_arg(@[si::structure-name], s, @[structure]); |
| 93 |
@(return ECL_STRUCT_NAME(s)); |
| 94 |
} |
| 95 |
|
| 96 |
cl_object |
| 97 |
si_structure_ref(cl_object x, cl_object type, cl_object index) |
| 98 |
{ |
| 99 |
if (ecl_unlikely(ecl_t_of(x) != T_STRUCTURE || |
| 100 |
!structure_subtypep(ECL_STRUCT_TYPE(x), type))) |
| 101 |
FEwrong_type_nth_arg(@[si::structure-ref], 1, x, type); |
| 102 |
@(return ECL_STRUCT_SLOT(x, ecl_fixnum(index))); |
| 103 |
} |
| 104 |
|
| 105 |
cl_object |
| 106 |
ecl_structure_ref(cl_object x, cl_object type, cl_fixnum n) |
| 107 |
{ |
| 108 |
|
| 109 |
if (ecl_unlikely(ecl_t_of(x) != T_STRUCTURE || |
| 110 |
!structure_subtypep(ECL_STRUCT_TYPE(x), type))) |
| 111 |
FEwrong_type_nth_arg(@[si::structure-ref], 1, x, type); |
| 112 |
return(ECL_STRUCT_SLOT(x, n)); |
| 113 |
} |
| 114 |
|
| 115 |
cl_object |
| 116 |
si_structure_set(cl_object x, cl_object type, cl_object index, cl_object val) |
| 117 |
{ |
| 118 |
if (ecl_unlikely(ecl_t_of(x) != T_STRUCTURE || |
| 119 |
!structure_subtypep(ECL_STRUCT_TYPE(x), type))) |
| 120 |
FEwrong_type_nth_arg(@[si::structure-set], 1, x, type); |
| 121 |
ECL_STRUCT_SLOT(x, ecl_fixnum(index)) = val; |
| 122 |
@(return val); |
| 123 |
} |
| 124 |
|
| 125 |
cl_object |
| 126 |
ecl_structure_set(cl_object x, cl_object type, cl_fixnum n, cl_object v) |
| 127 |
{ |
| 128 |
if (ecl_unlikely(ecl_t_of(x) != T_STRUCTURE || |
| 129 |
!structure_subtypep(ECL_STRUCT_TYPE(x), type))) |
| 130 |
FEwrong_type_nth_arg(@[si::structure-set], 1, x, type); |
| 131 |
ECL_STRUCT_SLOT(x, n) = v; |
| 132 |
return(v); |
| 133 |
} |
| 134 |
|
| 135 |
#ifdef ECL_THREADS |
| 136 |
cl_object |
| 137 |
mp_compare_and_swap_structure(cl_object x, cl_object type, cl_object index, cl_object old, cl_object new) |
| 138 |
{ |
| 139 |
if (ecl_unlikely(ecl_t_of(x) != T_STRUCTURE || |
| 140 |
!structure_subtypep(ECL_STRUCT_TYPE(x), type))) |
| 141 |
FEwrong_type_nth_arg(@[mp::compare-and-swap-structure], 1, x, type); |
| 142 |
return ecl_compare_and_swap(&(ECL_STRUCT_SLOT(x, ecl_fixnum(index))), old, new); |
| 143 |
} |
| 144 |
#endif |
| 145 |
|
| 146 |
cl_object |
| 147 |
si_structurep(cl_object s) |
| 148 |
{ |
| 149 |
if (ECL_INSTANCEP(s) && |
| 150 |
structure_subtypep(ECL_CLASS_OF(s), @'structure-object')) |
| 151 |
return ECL_T; |
| 152 |
else |
| 153 |
return ECL_NIL; |
| 154 |
} |