paul@353 | 1 | /* Common operations. |
paul@353 | 2 | |
paul@353 | 3 | Copyright (C) 2015, 2016 Paul Boddie <paul@boddie.org.uk> |
paul@353 | 4 | |
paul@353 | 5 | This program is free software; you can redistribute it and/or modify it under |
paul@353 | 6 | the terms of the GNU General Public License as published by the Free Software |
paul@353 | 7 | Foundation; either version 3 of the License, or (at your option) any later |
paul@353 | 8 | version. |
paul@353 | 9 | |
paul@353 | 10 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@353 | 11 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@353 | 12 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@353 | 13 | details. |
paul@353 | 14 | |
paul@353 | 15 | You should have received a copy of the GNU General Public License along with |
paul@353 | 16 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@353 | 17 | */ |
paul@126 | 18 | |
paul@433 | 19 | #include "gc.h" /* GC_MALLOC, GC_REALLOC */ |
paul@126 | 20 | #include "ops.h" |
paul@284 | 21 | #include "progops.h" /* for raising errors */ |
paul@126 | 22 | #include "progconsts.h" |
paul@126 | 23 | #include "progtypes.h" |
paul@126 | 24 | |
paul@126 | 25 | /* Direct access and manipulation of static objects. */ |
paul@126 | 26 | |
paul@200 | 27 | __attr __load_static(__ref parent, __ref obj) |
paul@126 | 28 | { |
paul@200 | 29 | __attr out = {.context=parent, .value=obj}; |
paul@126 | 30 | return out; |
paul@126 | 31 | } |
paul@126 | 32 | |
paul@126 | 33 | /* Direct retrieval operations, returning and setting attributes. */ |
paul@126 | 34 | |
paul@126 | 35 | __attr __load_via_object(__ref obj, int pos) |
paul@126 | 36 | { |
paul@126 | 37 | return obj->attrs[pos]; |
paul@126 | 38 | } |
paul@126 | 39 | |
paul@126 | 40 | __attr __load_via_class(__ref obj, int pos) |
paul@126 | 41 | { |
paul@126 | 42 | return __load_via_object(__get_class(obj), pos); |
paul@126 | 43 | } |
paul@126 | 44 | |
paul@126 | 45 | __attr __get_class_and_load(__ref obj, int pos) |
paul@126 | 46 | { |
paul@153 | 47 | if (__is_instance(obj)) |
paul@153 | 48 | return __load_via_class(obj, pos); |
paul@153 | 49 | else |
paul@126 | 50 | return __load_via_object(obj, pos); |
paul@126 | 51 | } |
paul@126 | 52 | |
paul@126 | 53 | /* Direct storage operations. */ |
paul@126 | 54 | |
paul@126 | 55 | int __store_via_object(__ref obj, int pos, __attr value) |
paul@126 | 56 | { |
paul@126 | 57 | obj->attrs[pos] = value; |
paul@126 | 58 | return 1; |
paul@126 | 59 | } |
paul@126 | 60 | |
paul@252 | 61 | int __get_class_and_store(__ref obj, int pos, __attr value) |
paul@252 | 62 | { |
paul@252 | 63 | /* Forbid class-relative assignments. */ |
paul@252 | 64 | |
paul@252 | 65 | __raise_type_error(); |
paul@252 | 66 | return 0; |
paul@252 | 67 | } |
paul@252 | 68 | |
paul@126 | 69 | /* Introspection. */ |
paul@126 | 70 | |
paul@126 | 71 | int __is_instance(__ref obj) |
paul@126 | 72 | { |
paul@126 | 73 | return obj->pos == __INSTANCEPOS; |
paul@126 | 74 | } |
paul@126 | 75 | |
paul@274 | 76 | int __is_type_instance(__ref obj) |
paul@274 | 77 | { |
paul@274 | 78 | return __HASATTR(__get_class(obj), __TYPE_CLASS_POS, __TYPE_CLASS_CODE); |
paul@274 | 79 | } |
paul@274 | 80 | |
paul@126 | 81 | __ref __get_class(__ref obj) |
paul@126 | 82 | { |
paul@126 | 83 | return __load_via_object(obj, __pos___class__).value; |
paul@126 | 84 | } |
paul@126 | 85 | |
paul@231 | 86 | __attr __get_class_attr(__ref obj) |
paul@231 | 87 | { |
paul@231 | 88 | return __load_via_object(obj, __pos___class__); |
paul@231 | 89 | } |
paul@231 | 90 | |
paul@126 | 91 | /* Attribute testing operations. */ |
paul@126 | 92 | |
paul@144 | 93 | __ref __test_specific_instance(__ref obj, __ref type) |
paul@126 | 94 | { |
paul@144 | 95 | return __get_class(obj) == type ? obj : 0; |
paul@126 | 96 | } |
paul@126 | 97 | |
paul@237 | 98 | __ref __test_specific_object(__ref obj, __ref type) |
paul@237 | 99 | { |
paul@237 | 100 | return __test_specific_type(obj, type) || __test_specific_instance(obj, type) ? obj : 0; |
paul@237 | 101 | } |
paul@237 | 102 | |
paul@237 | 103 | __ref __test_specific_type(__ref obj, __ref type) |
paul@237 | 104 | { |
paul@237 | 105 | return obj == type ? obj : 0; |
paul@237 | 106 | } |
paul@237 | 107 | |
paul@144 | 108 | __ref __test_common_instance(__ref obj, int pos, int code) |
paul@144 | 109 | { |
paul@144 | 110 | return __HASATTR(__get_class(obj), pos, code) ? obj : 0; |
paul@144 | 111 | } |
paul@144 | 112 | |
paul@144 | 113 | __ref __test_common_object(__ref obj, int pos, int code) |
paul@126 | 114 | { |
paul@144 | 115 | return __test_common_type(obj, pos, code) || __test_common_instance(obj, pos, code) ? obj : 0; |
paul@144 | 116 | } |
paul@144 | 117 | |
paul@144 | 118 | __ref __test_common_type(__ref obj, int pos, int code) |
paul@144 | 119 | { |
paul@144 | 120 | return __HASATTR(obj, pos, code) ? obj : 0; |
paul@126 | 121 | } |
paul@126 | 122 | |
paul@126 | 123 | /* Attribute testing and retrieval operations. */ |
paul@126 | 124 | |
paul@487 | 125 | __attr __check_and_load_via_object_null(__ref obj, int pos, int code) |
paul@233 | 126 | { |
paul@233 | 127 | if (__HASATTR(obj, pos, code)) |
paul@233 | 128 | return __load_via_object(obj, pos); |
paul@233 | 129 | else |
paul@233 | 130 | return __NULL; |
paul@233 | 131 | } |
paul@233 | 132 | |
paul@126 | 133 | __attr __check_and_load_via_class(__ref obj, int pos, int code) |
paul@126 | 134 | { |
paul@126 | 135 | return __check_and_load_via_object(__get_class(obj), pos, code); |
paul@126 | 136 | } |
paul@126 | 137 | |
paul@126 | 138 | __attr __check_and_load_via_object(__ref obj, int pos, int code) |
paul@126 | 139 | { |
paul@233 | 140 | if (__HASATTR(obj, pos, code)) |
paul@233 | 141 | return __load_via_object(obj, pos); |
paul@233 | 142 | |
paul@233 | 143 | __raise_type_error(); |
paul@233 | 144 | return __NULL; |
paul@126 | 145 | } |
paul@126 | 146 | |
paul@126 | 147 | __attr __check_and_load_via_any(__ref obj, int pos, int code) |
paul@126 | 148 | { |
paul@233 | 149 | __attr out = __check_and_load_via_object_null(obj, pos, code); |
paul@126 | 150 | if (out.value == 0) |
paul@126 | 151 | out = __check_and_load_via_class(obj, pos, code); |
paul@126 | 152 | return out; |
paul@126 | 153 | } |
paul@126 | 154 | |
paul@126 | 155 | /* Attribute testing and storage operations. */ |
paul@126 | 156 | |
paul@252 | 157 | int __check_and_store_via_class(__ref obj, int pos, int code, __attr value) |
paul@252 | 158 | { |
paul@252 | 159 | /* Forbid class-relative assignments. */ |
paul@252 | 160 | |
paul@252 | 161 | __raise_type_error(); |
paul@252 | 162 | return 0; |
paul@252 | 163 | } |
paul@252 | 164 | |
paul@126 | 165 | int __check_and_store_via_object(__ref obj, int pos, int code, __attr value) |
paul@126 | 166 | { |
paul@126 | 167 | if (__HASATTR(obj, pos, code)) |
paul@126 | 168 | { |
paul@126 | 169 | __store_via_object(obj, pos, value); |
paul@126 | 170 | return 1; |
paul@126 | 171 | } |
paul@252 | 172 | |
paul@252 | 173 | /* No suitable attribute. */ |
paul@252 | 174 | |
paul@252 | 175 | __raise_type_error(); |
paul@126 | 176 | return 0; |
paul@126 | 177 | } |
paul@126 | 178 | |
paul@126 | 179 | int __check_and_store_via_any(__ref obj, int pos, int code, __attr value) |
paul@126 | 180 | { |
paul@126 | 181 | if (__check_and_store_via_object(obj, pos, code, value)) |
paul@126 | 182 | return 1; |
paul@252 | 183 | |
paul@252 | 184 | /* Forbid class-relative assignments. */ |
paul@252 | 185 | |
paul@252 | 186 | __raise_type_error(); |
paul@252 | 187 | return 0; |
paul@126 | 188 | } |
paul@126 | 189 | |
paul@126 | 190 | /* Context-related operations. */ |
paul@126 | 191 | |
paul@126 | 192 | __attr __test_context(__ref context, __attr attr) |
paul@126 | 193 | { |
paul@267 | 194 | /* Preserve any existing null or instance context. */ |
paul@230 | 195 | |
paul@267 | 196 | if ((attr.context == 0) || __is_instance(attr.context)) |
paul@126 | 197 | return attr; |
paul@235 | 198 | |
paul@235 | 199 | /* Test any instance context against the context employed by the |
paul@235 | 200 | attribute. */ |
paul@126 | 201 | |
paul@235 | 202 | if (__is_instance(context)) |
paul@477 | 203 | { |
paul@235 | 204 | if (__test_common_instance(context, __TYPEPOS(attr.context), __TYPECODE(attr.context))) |
paul@235 | 205 | return __replace_context(context, attr); |
paul@235 | 206 | else |
paul@235 | 207 | __raise_type_error(); |
paul@477 | 208 | } |
paul@235 | 209 | |
paul@274 | 210 | /* Test for access to a type class attribute using a type instance. */ |
paul@274 | 211 | |
paul@274 | 212 | if (__test_specific_type(attr.context, &__TYPE_CLASS_TYPE) && __is_type_instance(context)) |
paul@274 | 213 | return __replace_context(context, attr); |
paul@274 | 214 | |
paul@235 | 215 | /* Otherwise, preserve the attribute as retrieved. */ |
paul@235 | 216 | |
paul@235 | 217 | return attr; |
paul@126 | 218 | } |
paul@126 | 219 | |
paul@126 | 220 | __attr __replace_context(__ref context, __attr attr) |
paul@126 | 221 | { |
paul@126 | 222 | __attr out; |
paul@126 | 223 | |
paul@126 | 224 | /* Set the context. */ |
paul@126 | 225 | |
paul@126 | 226 | out.context = context; |
paul@126 | 227 | |
paul@126 | 228 | /* Reference a callable version of the attribute by obtaining the bound |
paul@126 | 229 | method reference from the __fn__ special attribute. */ |
paul@126 | 230 | |
paul@126 | 231 | out.value = __load_via_object(attr.value, __ATTRPOS(__fn__)).b; |
paul@126 | 232 | return out; |
paul@126 | 233 | } |
paul@126 | 234 | |
paul@126 | 235 | __attr __update_context(__ref context, __attr attr) |
paul@126 | 236 | { |
paul@477 | 237 | __attr out = {.context=context, .fn=attr.fn}; |
paul@126 | 238 | return out; |
paul@126 | 239 | } |
paul@126 | 240 | |
paul@126 | 241 | /* Basic structure tests. */ |
paul@126 | 242 | |
paul@126 | 243 | int __WITHIN(__ref obj, int pos) |
paul@126 | 244 | { |
paul@126 | 245 | return pos < obj->table->size; |
paul@126 | 246 | } |
paul@126 | 247 | |
paul@126 | 248 | int __HASATTR(__ref obj, int pos, int code) |
paul@126 | 249 | { |
paul@126 | 250 | return __WITHIN(obj, pos) && (obj->table->attrs[pos] == code); |
paul@126 | 251 | } |
paul@126 | 252 | |
paul@126 | 253 | /* Parameter position operations. */ |
paul@126 | 254 | |
paul@126 | 255 | int __HASPARAM(const __ptable *ptable, int ppos, int pcode) |
paul@126 | 256 | { |
paul@126 | 257 | __param param; |
paul@126 | 258 | |
paul@126 | 259 | if (ppos < ptable->size) |
paul@126 | 260 | { |
paul@126 | 261 | param = ptable->params[ppos]; |
paul@126 | 262 | if (param.code == pcode) |
paul@126 | 263 | return param.pos; |
paul@126 | 264 | } |
paul@126 | 265 | |
paul@126 | 266 | return -1; |
paul@126 | 267 | } |
paul@126 | 268 | |
paul@126 | 269 | /* Conversions. */ |
paul@126 | 270 | |
paul@126 | 271 | __attr __CONTEXT_AS_VALUE(__attr attr) |
paul@126 | 272 | { |
paul@126 | 273 | __attr out; |
paul@126 | 274 | out.context = attr.context; |
paul@126 | 275 | out.value = attr.context; |
paul@126 | 276 | return out; |
paul@126 | 277 | } |
paul@126 | 278 | |
paul@126 | 279 | /* Type testing. */ |
paul@126 | 280 | |
paul@144 | 281 | __ref __ISFUNC(__ref obj) |
paul@126 | 282 | { |
paul@126 | 283 | return __test_specific_instance(obj, &__FUNCTION_TYPE); |
paul@126 | 284 | } |
paul@126 | 285 | |
paul@126 | 286 | int __ISNULL(__attr value) |
paul@126 | 287 | { |
paul@143 | 288 | /* (value.context == __NULL.context) is superfluous */ |
paul@143 | 289 | return (value.value == 0); /* __NULL.value */ |
paul@126 | 290 | } |
paul@126 | 291 | |
paul@126 | 292 | /* Attribute codes and positions for type objects. */ |
paul@126 | 293 | |
paul@126 | 294 | unsigned int __TYPECODE(__ref obj) |
paul@126 | 295 | { |
paul@126 | 296 | return obj->table->attrs[obj->pos]; |
paul@126 | 297 | } |
paul@126 | 298 | |
paul@126 | 299 | unsigned int __TYPEPOS(__ref obj) |
paul@126 | 300 | { |
paul@126 | 301 | return obj->pos; |
paul@126 | 302 | } |
paul@151 | 303 | |
paul@260 | 304 | /* Memory allocation. */ |
paul@260 | 305 | |
paul@260 | 306 | void *__ALLOCATE(size_t nmemb, size_t size) |
paul@260 | 307 | { |
paul@433 | 308 | void *ptr = GC_MALLOC(nmemb * size); /* sets memory to zero */ |
paul@260 | 309 | if (ptr == NULL) |
paul@260 | 310 | __raise_memory_error(); |
paul@260 | 311 | return ptr; |
paul@260 | 312 | } |
paul@260 | 313 | |
paul@260 | 314 | void *__REALLOCATE(void *ptr, size_t size) |
paul@260 | 315 | { |
paul@433 | 316 | void *nptr = GC_REALLOC(ptr, size); |
paul@260 | 317 | if (nptr == NULL) |
paul@260 | 318 | __raise_memory_error(); |
paul@260 | 319 | return nptr; |
paul@260 | 320 | } |
paul@260 | 321 | |
paul@151 | 322 | /* Copying of structures. */ |
paul@151 | 323 | |
paul@151 | 324 | __ref __COPY(__ref obj, int size) |
paul@151 | 325 | { |
paul@260 | 326 | __ref copy = (__ref) __ALLOCATE(1, size); |
paul@151 | 327 | memcpy(copy, obj, size); |
paul@151 | 328 | return copy; |
paul@151 | 329 | } |