paul@353 | 1 | /* Common operations. |
paul@353 | 2 | |
paul@523 | 3 | Copyright (C) 2015, 2016, 2017 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@649 | 20 | #include "types.h" |
paul@126 | 21 | #include "ops.h" |
paul@284 | 22 | #include "progops.h" /* for raising errors */ |
paul@126 | 23 | #include "progconsts.h" |
paul@126 | 24 | #include "progtypes.h" |
paul@126 | 25 | |
paul@126 | 26 | /* Direct access and manipulation of static objects. */ |
paul@126 | 27 | |
paul@577 | 28 | __attr __load_static_ignore(__ref obj) |
paul@577 | 29 | { |
paul@577 | 30 | return (__attr) {.value=obj}; |
paul@577 | 31 | } |
paul@577 | 32 | |
paul@577 | 33 | __attr __load_static_replace(__ref context, __ref obj) |
paul@126 | 34 | { |
paul@577 | 35 | return __update_context(context, (__attr) {.value=obj}); |
paul@577 | 36 | } |
paul@577 | 37 | |
paul@577 | 38 | __attr __load_static_test(__ref context, __ref obj) |
paul@577 | 39 | { |
paul@577 | 40 | return __test_context(context, (__attr) {.value=obj}); |
paul@126 | 41 | } |
paul@126 | 42 | |
paul@126 | 43 | /* Direct retrieval operations, returning and setting attributes. */ |
paul@126 | 44 | |
paul@624 | 45 | __attr __load_via_object__(__ref obj, int pos) |
paul@126 | 46 | { |
paul@126 | 47 | return obj->attrs[pos]; |
paul@126 | 48 | } |
paul@126 | 49 | |
paul@624 | 50 | __attr __load_via_class__(__ref obj, int pos) |
paul@126 | 51 | { |
paul@624 | 52 | return __load_via_object__(__get_class(obj), pos); |
paul@126 | 53 | } |
paul@126 | 54 | |
paul@624 | 55 | __attr __get_class_and_load__(__ref obj, int pos) |
paul@126 | 56 | { |
paul@153 | 57 | if (__is_instance(obj)) |
paul@624 | 58 | return __load_via_class__(obj, pos); |
paul@153 | 59 | else |
paul@624 | 60 | return __load_via_object__(obj, pos); |
paul@126 | 61 | } |
paul@126 | 62 | |
paul@126 | 63 | /* Direct storage operations. */ |
paul@126 | 64 | |
paul@624 | 65 | int __store_via_object__(__ref obj, int pos, __attr value) |
paul@126 | 66 | { |
paul@126 | 67 | obj->attrs[pos] = value; |
paul@126 | 68 | return 1; |
paul@126 | 69 | } |
paul@126 | 70 | |
paul@624 | 71 | int __get_class_and_store__(__ref obj, int pos, __attr value) |
paul@252 | 72 | { |
paul@252 | 73 | /* Forbid class-relative assignments. */ |
paul@252 | 74 | |
paul@252 | 75 | __raise_type_error(); |
paul@252 | 76 | return 0; |
paul@252 | 77 | } |
paul@252 | 78 | |
paul@126 | 79 | /* Introspection. */ |
paul@126 | 80 | |
paul@126 | 81 | int __is_instance(__ref obj) |
paul@126 | 82 | { |
paul@126 | 83 | return obj->pos == __INSTANCEPOS; |
paul@126 | 84 | } |
paul@126 | 85 | |
paul@274 | 86 | int __is_type_instance(__ref obj) |
paul@274 | 87 | { |
paul@274 | 88 | return __HASATTR(__get_class(obj), __TYPE_CLASS_POS, __TYPE_CLASS_CODE); |
paul@274 | 89 | } |
paul@274 | 90 | |
paul@126 | 91 | __ref __get_class(__ref obj) |
paul@126 | 92 | { |
paul@624 | 93 | return __load_via_object(obj, __class__).value; |
paul@126 | 94 | } |
paul@126 | 95 | |
paul@231 | 96 | __attr __get_class_attr(__ref obj) |
paul@231 | 97 | { |
paul@624 | 98 | return __load_via_object(obj, __class__); |
paul@231 | 99 | } |
paul@231 | 100 | |
paul@126 | 101 | /* Attribute testing operations. */ |
paul@126 | 102 | |
paul@144 | 103 | __ref __test_specific_instance(__ref obj, __ref type) |
paul@126 | 104 | { |
paul@144 | 105 | return __get_class(obj) == type ? obj : 0; |
paul@126 | 106 | } |
paul@126 | 107 | |
paul@237 | 108 | __ref __test_specific_object(__ref obj, __ref type) |
paul@237 | 109 | { |
paul@237 | 110 | return __test_specific_type(obj, type) || __test_specific_instance(obj, type) ? obj : 0; |
paul@237 | 111 | } |
paul@237 | 112 | |
paul@237 | 113 | __ref __test_specific_type(__ref obj, __ref type) |
paul@237 | 114 | { |
paul@237 | 115 | return obj == type ? obj : 0; |
paul@237 | 116 | } |
paul@237 | 117 | |
paul@624 | 118 | __ref __test_common_instance__(__ref obj, int pos, int code) |
paul@144 | 119 | { |
paul@144 | 120 | return __HASATTR(__get_class(obj), pos, code) ? obj : 0; |
paul@144 | 121 | } |
paul@144 | 122 | |
paul@624 | 123 | __ref __test_common_object__(__ref obj, int pos, int code) |
paul@126 | 124 | { |
paul@624 | 125 | return __test_common_type__(obj, pos, code) || __test_common_instance__(obj, pos, code) ? obj : 0; |
paul@144 | 126 | } |
paul@144 | 127 | |
paul@624 | 128 | __ref __test_common_type__(__ref obj, int pos, int code) |
paul@144 | 129 | { |
paul@144 | 130 | return __HASATTR(obj, pos, code) ? obj : 0; |
paul@126 | 131 | } |
paul@126 | 132 | |
paul@126 | 133 | /* Attribute testing and retrieval operations. */ |
paul@126 | 134 | |
paul@487 | 135 | __attr __check_and_load_via_object_null(__ref obj, int pos, int code) |
paul@233 | 136 | { |
paul@233 | 137 | if (__HASATTR(obj, pos, code)) |
paul@624 | 138 | return __load_via_object__(obj, pos); |
paul@233 | 139 | else |
paul@233 | 140 | return __NULL; |
paul@233 | 141 | } |
paul@233 | 142 | |
paul@624 | 143 | __attr __check_and_load_via_class__(__ref obj, int pos, int code) |
paul@126 | 144 | { |
paul@624 | 145 | return __check_and_load_via_object__(__get_class(obj), pos, code); |
paul@126 | 146 | } |
paul@126 | 147 | |
paul@624 | 148 | __attr __check_and_load_via_object__(__ref obj, int pos, int code) |
paul@126 | 149 | { |
paul@233 | 150 | if (__HASATTR(obj, pos, code)) |
paul@624 | 151 | return __load_via_object__(obj, pos); |
paul@233 | 152 | |
paul@233 | 153 | __raise_type_error(); |
paul@233 | 154 | return __NULL; |
paul@126 | 155 | } |
paul@126 | 156 | |
paul@624 | 157 | __attr __check_and_load_via_any__(__ref obj, int pos, int code) |
paul@126 | 158 | { |
paul@233 | 159 | __attr out = __check_and_load_via_object_null(obj, pos, code); |
paul@126 | 160 | if (out.value == 0) |
paul@624 | 161 | out = __check_and_load_via_class__(obj, pos, code); |
paul@126 | 162 | return out; |
paul@126 | 163 | } |
paul@126 | 164 | |
paul@126 | 165 | /* Attribute testing and storage operations. */ |
paul@126 | 166 | |
paul@624 | 167 | int __check_and_store_via_class__(__ref obj, int pos, int code, __attr value) |
paul@252 | 168 | { |
paul@252 | 169 | /* Forbid class-relative assignments. */ |
paul@252 | 170 | |
paul@252 | 171 | __raise_type_error(); |
paul@252 | 172 | return 0; |
paul@252 | 173 | } |
paul@252 | 174 | |
paul@624 | 175 | int __check_and_store_via_object__(__ref obj, int pos, int code, __attr value) |
paul@126 | 176 | { |
paul@126 | 177 | if (__HASATTR(obj, pos, code)) |
paul@126 | 178 | { |
paul@624 | 179 | __store_via_object__(obj, pos, value); |
paul@126 | 180 | return 1; |
paul@126 | 181 | } |
paul@252 | 182 | |
paul@252 | 183 | /* No suitable attribute. */ |
paul@252 | 184 | |
paul@252 | 185 | __raise_type_error(); |
paul@126 | 186 | return 0; |
paul@126 | 187 | } |
paul@126 | 188 | |
paul@624 | 189 | int __check_and_store_via_any__(__ref obj, int pos, int code, __attr value) |
paul@126 | 190 | { |
paul@624 | 191 | if (__check_and_store_via_object__(obj, pos, code, value)) |
paul@126 | 192 | return 1; |
paul@252 | 193 | |
paul@252 | 194 | /* Forbid class-relative assignments. */ |
paul@252 | 195 | |
paul@252 | 196 | __raise_type_error(); |
paul@252 | 197 | return 0; |
paul@126 | 198 | } |
paul@126 | 199 | |
paul@126 | 200 | /* Context-related operations. */ |
paul@126 | 201 | |
paul@594 | 202 | int __test_context_update(__ref context, __attr attr) |
paul@126 | 203 | { |
paul@594 | 204 | /* Return whether the context should be updated for the attribute. */ |
paul@594 | 205 | |
paul@577 | 206 | __ref attrcontext = __CONTEXT_AS_VALUE(attr).value; |
paul@577 | 207 | |
paul@267 | 208 | /* Preserve any existing null or instance context. */ |
paul@230 | 209 | |
paul@577 | 210 | if ((attrcontext == 0) || __is_instance(attrcontext)) |
paul@594 | 211 | return 0; |
paul@235 | 212 | |
paul@235 | 213 | /* Test any instance context against the context employed by the |
paul@235 | 214 | attribute. */ |
paul@126 | 215 | |
paul@235 | 216 | if (__is_instance(context)) |
paul@477 | 217 | { |
paul@601 | 218 | /* Obtain the special class attribute position and code identifying the |
paul@601 | 219 | attribute context's class, inspecting the context instance for |
paul@601 | 220 | compatibility. */ |
paul@601 | 221 | |
paul@624 | 222 | if (__test_common_instance__(context, __TYPEPOS(attrcontext), __TYPECODE(attrcontext))) |
paul@594 | 223 | return 1; |
paul@235 | 224 | else |
paul@235 | 225 | __raise_type_error(); |
paul@477 | 226 | } |
paul@235 | 227 | |
paul@274 | 228 | /* Test for access to a type class attribute using a type instance. */ |
paul@274 | 229 | |
paul@577 | 230 | if (__test_specific_type(attrcontext, &__TYPE_CLASS_TYPE) && __is_type_instance(context)) |
paul@594 | 231 | return 1; |
paul@274 | 232 | |
paul@235 | 233 | /* Otherwise, preserve the attribute as retrieved. */ |
paul@235 | 234 | |
paul@594 | 235 | return 0; |
paul@594 | 236 | } |
paul@594 | 237 | |
paul@594 | 238 | __attr __test_context(__ref context, __attr attr) |
paul@594 | 239 | { |
paul@594 | 240 | /* Update the context or return the unchanged attribute. */ |
paul@594 | 241 | |
paul@594 | 242 | if (__test_context_update(context, attr)) |
paul@594 | 243 | return __update_context(context, attr); |
paul@594 | 244 | else |
paul@594 | 245 | return attr; |
paul@126 | 246 | } |
paul@126 | 247 | |
paul@126 | 248 | __attr __update_context(__ref context, __attr attr) |
paul@126 | 249 | { |
paul@577 | 250 | return __new_wrapper(context, attr); |
paul@126 | 251 | } |
paul@126 | 252 | |
paul@602 | 253 | __attr __test_context_revert(int target, __ref context, __attr attr, __ref contexts[]) |
paul@602 | 254 | { |
paul@602 | 255 | /* Revert the local context to that employed by the attribute if the |
paul@602 | 256 | supplied context is not appropriate. */ |
paul@602 | 257 | |
paul@602 | 258 | if (!__test_context_update(context, attr)) |
paul@602 | 259 | contexts[target] = __CONTEXT_AS_VALUE(attr).value; |
paul@602 | 260 | return attr; |
paul@602 | 261 | } |
paul@602 | 262 | |
paul@602 | 263 | __attr __test_context_static(int target, __ref context, __ref value, __ref contexts[]) |
paul@602 | 264 | { |
paul@602 | 265 | /* Set the local context to the specified context if appropriate. */ |
paul@602 | 266 | |
paul@602 | 267 | if (__test_context_update(context, (__attr) {.value=value})) |
paul@602 | 268 | contexts[target] = context; |
paul@602 | 269 | return (__attr) {.value=value}; |
paul@602 | 270 | } |
paul@602 | 271 | |
paul@523 | 272 | /* Context testing for invocations. */ |
paul@523 | 273 | |
paul@577 | 274 | int __type_method_invocation(__ref context, __attr target) |
paul@523 | 275 | { |
paul@577 | 276 | __ref targetcontext = __CONTEXT_AS_VALUE(target).value; |
paul@523 | 277 | |
paul@523 | 278 | /* Require instances, not classes, where methods are function instances. */ |
paul@523 | 279 | |
paul@577 | 280 | if (!__is_instance(target.value)) |
paul@523 | 281 | return 0; |
paul@523 | 282 | |
paul@577 | 283 | /* Access the context of the callable and test if it is the type object. */ |
paul@523 | 284 | |
paul@577 | 285 | return ((targetcontext != 0) && __test_specific_type(targetcontext, &__TYPE_CLASS_TYPE) && __is_type_instance(context)); |
paul@523 | 286 | } |
paul@523 | 287 | |
paul@577 | 288 | __attr __unwrap_callable(__attr callable) |
paul@523 | 289 | { |
paul@577 | 290 | __attr value = __check_and_load_via_object_null(callable.value, __ATTRPOS(__value__), __ATTRCODE(__value__)); |
paul@577 | 291 | return value.value ? value : callable; |
paul@577 | 292 | } |
paul@577 | 293 | |
paul@577 | 294 | __attr (*__get_function(__ref context, __attr target))(__attr[]) |
paul@577 | 295 | { |
paul@577 | 296 | target = __unwrap_callable(target); |
paul@577 | 297 | |
paul@523 | 298 | /* Require null or instance contexts for functions and methods respectively, |
paul@523 | 299 | or type instance contexts for type methods. */ |
paul@523 | 300 | |
paul@577 | 301 | if ((context == 0) || __is_instance(context) || __type_method_invocation(context, target)) |
paul@624 | 302 | return __load_via_object(target.value, __fn__).fn; |
paul@523 | 303 | else |
paul@577 | 304 | return __unbound_method; |
paul@523 | 305 | } |
paul@523 | 306 | |
paul@577 | 307 | __attr (*__check_and_get_function(__ref context, __attr target))(__attr[]) |
paul@523 | 308 | { |
paul@577 | 309 | target = __unwrap_callable(target); |
paul@577 | 310 | |
paul@523 | 311 | /* Require null or instance contexts for functions and methods respectively, |
paul@523 | 312 | or type instance contexts for type methods. */ |
paul@523 | 313 | |
paul@577 | 314 | if ((context == 0) || __is_instance(context) || __type_method_invocation(context, target)) |
paul@624 | 315 | return __check_and_load_via_object__(target.value, __ATTRPOS(__fn__), __ATTRCODE(__fn__)).fn; |
paul@523 | 316 | else |
paul@577 | 317 | return __unbound_method; |
paul@523 | 318 | } |
paul@523 | 319 | |
paul@126 | 320 | /* Basic structure tests. */ |
paul@126 | 321 | |
paul@126 | 322 | int __WITHIN(__ref obj, int pos) |
paul@126 | 323 | { |
paul@126 | 324 | return pos < obj->table->size; |
paul@126 | 325 | } |
paul@126 | 326 | |
paul@126 | 327 | int __HASATTR(__ref obj, int pos, int code) |
paul@126 | 328 | { |
paul@126 | 329 | return __WITHIN(obj, pos) && (obj->table->attrs[pos] == code); |
paul@126 | 330 | } |
paul@126 | 331 | |
paul@126 | 332 | /* Parameter position operations. */ |
paul@126 | 333 | |
paul@126 | 334 | int __HASPARAM(const __ptable *ptable, int ppos, int pcode) |
paul@126 | 335 | { |
paul@126 | 336 | __param param; |
paul@126 | 337 | |
paul@126 | 338 | if (ppos < ptable->size) |
paul@126 | 339 | { |
paul@126 | 340 | param = ptable->params[ppos]; |
paul@126 | 341 | if (param.code == pcode) |
paul@126 | 342 | return param.pos; |
paul@126 | 343 | } |
paul@126 | 344 | |
paul@126 | 345 | return -1; |
paul@126 | 346 | } |
paul@126 | 347 | |
paul@126 | 348 | /* Conversions. */ |
paul@126 | 349 | |
paul@126 | 350 | __attr __CONTEXT_AS_VALUE(__attr attr) |
paul@126 | 351 | { |
paul@577 | 352 | return __check_and_load_via_object_null(attr.value, __ATTRPOS(__context__), __ATTRCODE(__context__)); |
paul@126 | 353 | } |
paul@126 | 354 | |
paul@126 | 355 | /* Type testing. */ |
paul@126 | 356 | |
paul@144 | 357 | __ref __ISFUNC(__ref obj) |
paul@126 | 358 | { |
paul@126 | 359 | return __test_specific_instance(obj, &__FUNCTION_TYPE); |
paul@126 | 360 | } |
paul@126 | 361 | |
paul@126 | 362 | int __ISNULL(__attr value) |
paul@126 | 363 | { |
paul@143 | 364 | return (value.value == 0); /* __NULL.value */ |
paul@126 | 365 | } |
paul@126 | 366 | |
paul@126 | 367 | /* Attribute codes and positions for type objects. */ |
paul@126 | 368 | |
paul@126 | 369 | unsigned int __TYPECODE(__ref obj) |
paul@126 | 370 | { |
paul@126 | 371 | return obj->table->attrs[obj->pos]; |
paul@126 | 372 | } |
paul@126 | 373 | |
paul@126 | 374 | unsigned int __TYPEPOS(__ref obj) |
paul@126 | 375 | { |
paul@126 | 376 | return obj->pos; |
paul@126 | 377 | } |
paul@151 | 378 | |
paul@260 | 379 | /* Memory allocation. */ |
paul@260 | 380 | |
paul@260 | 381 | void *__ALLOCATE(size_t nmemb, size_t size) |
paul@260 | 382 | { |
paul@433 | 383 | void *ptr = GC_MALLOC(nmemb * size); /* sets memory to zero */ |
paul@260 | 384 | if (ptr == NULL) |
paul@260 | 385 | __raise_memory_error(); |
paul@260 | 386 | return ptr; |
paul@260 | 387 | } |
paul@260 | 388 | |
paul@260 | 389 | void *__REALLOCATE(void *ptr, size_t size) |
paul@260 | 390 | { |
paul@433 | 391 | void *nptr = GC_REALLOC(ptr, size); |
paul@260 | 392 | if (nptr == NULL) |
paul@260 | 393 | __raise_memory_error(); |
paul@260 | 394 | return nptr; |
paul@260 | 395 | } |
paul@260 | 396 | |
paul@151 | 397 | /* Copying of structures. */ |
paul@151 | 398 | |
paul@151 | 399 | __ref __COPY(__ref obj, int size) |
paul@151 | 400 | { |
paul@260 | 401 | __ref copy = (__ref) __ALLOCATE(1, size); |
paul@151 | 402 | memcpy(copy, obj, size); |
paul@151 | 403 | return copy; |
paul@151 | 404 | } |