| 1 |
/* -*- Mode: C; c-basic-offset: 2; indent-tabs-mode: nil -*- */ |
| 2 |
/* vim: set filetype=c tabstop=2 shiftwidth=2 expandtab: */ |
| 3 |
|
| 4 |
/* |
| 5 |
* accessor.d - dispatch for slots |
| 6 |
* |
| 7 |
* Copyright (c) 2011 Juan Jose Garcia Ripoll |
| 8 |
* |
| 9 |
* See file 'LICENSE' for the copyright details. |
| 10 |
* |
| 11 |
*/ |
| 12 |
|
| 13 |
#include <string.h> |
| 14 |
#include <ecl/ecl.h> |
| 15 |
#include <ecl/ecl-inl.h> |
| 16 |
#include <ecl/internal.h> |
| 17 |
#include <ecl/cache.h> |
| 18 |
|
| 19 |
static void |
| 20 |
no_applicable_method(cl_env_ptr env, cl_object gfun, cl_object args) |
| 21 |
{ |
| 22 |
env->values[0] = cl_apply(3, @'no-applicable-method', gfun, args); |
| 23 |
} |
| 24 |
|
| 25 |
static cl_object |
| 26 |
fill_spec_vector(cl_object vector, cl_object gfun, cl_object instance) |
| 27 |
{ |
| 28 |
cl_object *argtype = vector->vector.self.t; |
| 29 |
argtype[0] = gfun; |
| 30 |
argtype[1] = ECL_CLASS_OF(instance); |
| 31 |
vector->vector.fillp = 2; |
| 32 |
return vector; |
| 33 |
} |
| 34 |
|
| 35 |
static cl_object |
| 36 |
slot_method_name(cl_object gfun, cl_object args) |
| 37 |
{ |
| 38 |
cl_object methods = _ecl_funcall3(@'compute-applicable-methods', |
| 39 |
gfun, args); |
| 40 |
unlikely_if (Null(methods)) { |
| 41 |
return OBJNULL; |
| 42 |
} else { |
| 43 |
cl_object first = ECL_CONS_CAR(methods); |
| 44 |
cl_object slotd = _ecl_funcall3(@'slot-value', first, |
| 45 |
@'clos::slot-definition'); |
| 46 |
return _ecl_funcall3(@'slot-value', slotd, @'clos::name'); |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
static cl_object |
| 51 |
slot_method_index(cl_object gfun, cl_object instance, cl_object args) |
| 52 |
{ |
| 53 |
cl_object slot_name = slot_method_name(gfun, args); |
| 54 |
unlikely_if (slot_name == OBJNULL) |
| 55 |
return OBJNULL; |
| 56 |
else { |
| 57 |
cl_object table = _ecl_funcall3(@'slot-value', |
| 58 |
ECL_CLASS_OF(instance), |
| 59 |
@'clos::location-table'); |
| 60 |
/* The class might not be a standard class. This happens |
| 61 |
* when a nonstandard class inherits from a standard class |
| 62 |
* and does not add any new slot accessor. |
| 63 |
*/ |
| 64 |
unlikely_if (Null(table)) |
| 65 |
return slot_name; |
| 66 |
return ecl_gethash_safe(slot_name, table, OBJNULL); |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
static ecl_cache_record_ptr |
| 71 |
search_slot_index(const cl_env_ptr env, cl_object gfun, cl_object instance) |
| 72 |
{ |
| 73 |
ecl_cache_ptr cache = env->slot_cache; |
| 74 |
ecl_cache_record_ptr ret; |
| 75 |
ECL_WITHOUT_INTERRUPTS_BEGIN(env) { |
| 76 |
fill_spec_vector(cache->keys, gfun, instance); |
| 77 |
ret = ecl_search_cache(cache); |
| 78 |
} ECL_WITHOUT_INTERRUPTS_END; |
| 79 |
return ret; |
| 80 |
} |
| 81 |
|
| 82 |
static ecl_cache_record_ptr |
| 83 |
add_new_index(const cl_env_ptr env, cl_object gfun, cl_object instance, cl_object args) |
| 84 |
{ |
| 85 |
/* The keys and the cache may change while we compute the |
| 86 |
* applicable methods. We must save the keys and recompute the |
| 87 |
* cache location if it was filled. */ |
| 88 |
cl_object index = slot_method_index(gfun, instance, args); |
| 89 |
unlikely_if (index == OBJNULL) { |
| 90 |
no_applicable_method(env, gfun, args); |
| 91 |
return 0; |
| 92 |
} |
| 93 |
{ |
| 94 |
ecl_cache_record_ptr e; |
| 95 |
ecl_cache_ptr cache = env->slot_cache; |
| 96 |
ECL_WITHOUT_INTERRUPTS_BEGIN(env) { |
| 97 |
fill_spec_vector(cache->keys, gfun, instance); |
| 98 |
e = ecl_search_cache(cache); |
| 99 |
e->key = cl_copy_seq(cache->keys); |
| 100 |
e->value = index; |
| 101 |
} ECL_WITHOUT_INTERRUPTS_END; |
| 102 |
return e; |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
static void |
| 107 |
ensure_up_to_date_instance(cl_object instance) |
| 108 |
{ |
| 109 |
if (si_instance_obsolete_p(instance) == ECL_T) { |
| 110 |
_ecl_funcall2(@'clos::update-instance', instance); |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
cl_object |
| 115 |
ecl_slot_reader_dispatch(cl_narg narg, ... /* cl_object instance */) |
| 116 |
{ |
| 117 |
const cl_env_ptr env = ecl_process_env(); |
| 118 |
cl_object gfun = env->function; |
| 119 |
cl_object index, value; |
| 120 |
ecl_cache_record_ptr e; |
| 121 |
cl_object instance; |
| 122 |
|
| 123 |
unlikely_if (narg != 1) { |
| 124 |
FEwrong_num_arguments(gfun); |
| 125 |
} else { |
| 126 |
va_list args; |
| 127 |
va_start(args, narg); |
| 128 |
instance = va_arg(args, cl_object); |
| 129 |
va_end(args); |
| 130 |
} |
| 131 |
|
| 132 |
unlikely_if (!ECL_INSTANCEP(instance)) { |
| 133 |
no_applicable_method(env, gfun, ecl_list1(instance)); |
| 134 |
return env->values[0]; |
| 135 |
} |
| 136 |
|
| 137 |
e = search_slot_index(env, gfun, instance); |
| 138 |
unlikely_if (e->key == OBJNULL) { |
| 139 |
cl_object args = ecl_list1(instance); |
| 140 |
e = add_new_index(env, gfun, instance, args); |
| 141 |
/* no_applicable_method() was called */ |
| 142 |
unlikely_if (e == 0) { |
| 143 |
return env->values[0]; |
| 144 |
} |
| 145 |
} |
| 146 |
ensure_up_to_date_instance(instance); |
| 147 |
index = e->value; |
| 148 |
if (ECL_FIXNUMP(index)) { |
| 149 |
value = instance->instance.slots[ecl_fixnum(index)]; |
| 150 |
} else if (ecl_unlikely(!ECL_LISTP(index))) { |
| 151 |
value = cl_slot_value(instance, index); |
| 152 |
} else if (ecl_unlikely(Null(index))) { |
| 153 |
FEerror("Error when accessing method cache for ~A", 1, gfun); |
| 154 |
} else { |
| 155 |
value = ECL_CONS_CAR(index); |
| 156 |
} |
| 157 |
unlikely_if (value == ECL_UNBOUND) { |
| 158 |
cl_object slot_name = slot_method_name(gfun, ecl_list1(instance)); |
| 159 |
value = _ecl_funcall4(@'slot-unbound', |
| 160 |
ECL_CLASS_OF(instance), |
| 161 |
instance, |
| 162 |
slot_name); |
| 163 |
} |
| 164 |
@(return value); |
| 165 |
} |
| 166 |
|
| 167 |
cl_object |
| 168 |
ecl_slot_writer_dispatch(cl_narg narg, ... /* cl_object value, cl_object instance */) |
| 169 |
{ |
| 170 |
const cl_env_ptr env = ecl_process_env(); |
| 171 |
cl_object gfun = env->function; |
| 172 |
ecl_cache_record_ptr e; |
| 173 |
cl_object index; |
| 174 |
cl_object value, instance; |
| 175 |
|
| 176 |
unlikely_if (narg != 2) { |
| 177 |
FEwrong_num_arguments(gfun); |
| 178 |
} else { |
| 179 |
va_list args; |
| 180 |
va_start(args, narg); |
| 181 |
value = va_arg(args, cl_object); |
| 182 |
instance = va_arg(args, cl_object); |
| 183 |
va_end(args); |
| 184 |
} |
| 185 |
|
| 186 |
unlikely_if (!ECL_INSTANCEP(instance)) { |
| 187 |
no_applicable_method(env, gfun, cl_list(2, value, instance)); |
| 188 |
return env->values[0]; |
| 189 |
} |
| 190 |
e = search_slot_index(env, gfun, instance); |
| 191 |
unlikely_if (e->key == OBJNULL) { |
| 192 |
cl_object args = cl_list(2, value, instance); |
| 193 |
e = add_new_index(env, gfun, instance, args); |
| 194 |
/* no_applicable_method() was called */ |
| 195 |
unlikely_if (e == 0) { |
| 196 |
return env->values[0]; |
| 197 |
} |
| 198 |
} |
| 199 |
index = e->value; |
| 200 |
if (ECL_FIXNUMP(index)) { |
| 201 |
instance->instance.slots[ecl_fixnum(index)] = value; |
| 202 |
} else if (ecl_unlikely(!ECL_LISTP(index))) { |
| 203 |
clos_slot_value_set(value, instance, index); |
| 204 |
} else if (ecl_unlikely(Null(index))) { |
| 205 |
FEerror("Error when accessing method cache for ~A", 1, gfun); |
| 206 |
} else { |
| 207 |
ECL_RPLACA(index, value); |
| 208 |
} |
| 209 |
@(return value); |
| 210 |
} |