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@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@523 | 24 | #include <stdio.h> |
paul@126 | 25 | |
paul@126 | 26 | /* Direct access and manipulation of static objects. */ |
paul@126 | 27 | |
paul@200 | 28 | __attr __load_static(__ref parent, __ref obj) |
paul@126 | 29 | { |
paul@200 | 30 | __attr out = {.context=parent, .value=obj}; |
paul@126 | 31 | return out; |
paul@126 | 32 | } |
paul@126 | 33 | |
paul@126 | 34 | /* Direct retrieval operations, returning and setting attributes. */ |
paul@126 | 35 | |
paul@126 | 36 | __attr __load_via_object(__ref obj, int pos) |
paul@126 | 37 | { |
paul@126 | 38 | return obj->attrs[pos]; |
paul@126 | 39 | } |
paul@126 | 40 | |
paul@126 | 41 | __attr __load_via_class(__ref obj, int pos) |
paul@126 | 42 | { |
paul@126 | 43 | return __load_via_object(__get_class(obj), pos); |
paul@126 | 44 | } |
paul@126 | 45 | |
paul@126 | 46 | __attr __get_class_and_load(__ref obj, int pos) |
paul@126 | 47 | { |
paul@153 | 48 | if (__is_instance(obj)) |
paul@153 | 49 | return __load_via_class(obj, pos); |
paul@153 | 50 | else |
paul@126 | 51 | return __load_via_object(obj, pos); |
paul@126 | 52 | } |
paul@126 | 53 | |
paul@126 | 54 | /* Direct storage operations. */ |
paul@126 | 55 | |
paul@126 | 56 | int __store_via_object(__ref obj, int pos, __attr value) |
paul@126 | 57 | { |
paul@126 | 58 | obj->attrs[pos] = value; |
paul@126 | 59 | return 1; |
paul@126 | 60 | } |
paul@126 | 61 | |
paul@252 | 62 | int __get_class_and_store(__ref obj, int pos, __attr value) |
paul@252 | 63 | { |
paul@252 | 64 | /* Forbid class-relative assignments. */ |
paul@252 | 65 | |
paul@252 | 66 | __raise_type_error(); |
paul@252 | 67 | return 0; |
paul@252 | 68 | } |
paul@252 | 69 | |
paul@126 | 70 | /* Introspection. */ |
paul@126 | 71 | |
paul@126 | 72 | int __is_instance(__ref obj) |
paul@126 | 73 | { |
paul@126 | 74 | return obj->pos == __INSTANCEPOS; |
paul@126 | 75 | } |
paul@126 | 76 | |
paul@274 | 77 | int __is_type_instance(__ref obj) |
paul@274 | 78 | { |
paul@274 | 79 | return __HASATTR(__get_class(obj), __TYPE_CLASS_POS, __TYPE_CLASS_CODE); |
paul@274 | 80 | } |
paul@274 | 81 | |
paul@126 | 82 | __ref __get_class(__ref obj) |
paul@126 | 83 | { |
paul@126 | 84 | return __load_via_object(obj, __pos___class__).value; |
paul@126 | 85 | } |
paul@126 | 86 | |
paul@231 | 87 | __attr __get_class_attr(__ref obj) |
paul@231 | 88 | { |
paul@231 | 89 | return __load_via_object(obj, __pos___class__); |
paul@231 | 90 | } |
paul@231 | 91 | |
paul@126 | 92 | /* Attribute testing operations. */ |
paul@126 | 93 | |
paul@144 | 94 | __ref __test_specific_instance(__ref obj, __ref type) |
paul@126 | 95 | { |
paul@144 | 96 | return __get_class(obj) == type ? obj : 0; |
paul@126 | 97 | } |
paul@126 | 98 | |
paul@237 | 99 | __ref __test_specific_object(__ref obj, __ref type) |
paul@237 | 100 | { |
paul@237 | 101 | return __test_specific_type(obj, type) || __test_specific_instance(obj, type) ? obj : 0; |
paul@237 | 102 | } |
paul@237 | 103 | |
paul@237 | 104 | __ref __test_specific_type(__ref obj, __ref type) |
paul@237 | 105 | { |
paul@237 | 106 | return obj == type ? obj : 0; |
paul@237 | 107 | } |
paul@237 | 108 | |
paul@144 | 109 | __ref __test_common_instance(__ref obj, int pos, int code) |
paul@144 | 110 | { |
paul@144 | 111 | return __HASATTR(__get_class(obj), pos, code) ? obj : 0; |
paul@144 | 112 | } |
paul@144 | 113 | |
paul@144 | 114 | __ref __test_common_object(__ref obj, int pos, int code) |
paul@126 | 115 | { |
paul@144 | 116 | return __test_common_type(obj, pos, code) || __test_common_instance(obj, pos, code) ? obj : 0; |
paul@144 | 117 | } |
paul@144 | 118 | |
paul@144 | 119 | __ref __test_common_type(__ref obj, int pos, int code) |
paul@144 | 120 | { |
paul@144 | 121 | return __HASATTR(obj, pos, code) ? obj : 0; |
paul@126 | 122 | } |
paul@126 | 123 | |
paul@126 | 124 | /* Attribute testing and retrieval operations. */ |
paul@126 | 125 | |
paul@487 | 126 | __attr __check_and_load_via_object_null(__ref obj, int pos, int code) |
paul@233 | 127 | { |
paul@233 | 128 | if (__HASATTR(obj, pos, code)) |
paul@233 | 129 | return __load_via_object(obj, pos); |
paul@233 | 130 | else |
paul@233 | 131 | return __NULL; |
paul@233 | 132 | } |
paul@233 | 133 | |
paul@126 | 134 | __attr __check_and_load_via_class(__ref obj, int pos, int code) |
paul@126 | 135 | { |
paul@126 | 136 | return __check_and_load_via_object(__get_class(obj), pos, code); |
paul@126 | 137 | } |
paul@126 | 138 | |
paul@126 | 139 | __attr __check_and_load_via_object(__ref obj, int pos, int code) |
paul@126 | 140 | { |
paul@233 | 141 | if (__HASATTR(obj, pos, code)) |
paul@233 | 142 | return __load_via_object(obj, pos); |
paul@233 | 143 | |
paul@233 | 144 | __raise_type_error(); |
paul@233 | 145 | return __NULL; |
paul@126 | 146 | } |
paul@126 | 147 | |
paul@126 | 148 | __attr __check_and_load_via_any(__ref obj, int pos, int code) |
paul@126 | 149 | { |
paul@233 | 150 | __attr out = __check_and_load_via_object_null(obj, pos, code); |
paul@126 | 151 | if (out.value == 0) |
paul@126 | 152 | out = __check_and_load_via_class(obj, pos, code); |
paul@126 | 153 | return out; |
paul@126 | 154 | } |
paul@126 | 155 | |
paul@126 | 156 | /* Attribute testing and storage operations. */ |
paul@126 | 157 | |
paul@252 | 158 | int __check_and_store_via_class(__ref obj, int pos, int code, __attr value) |
paul@252 | 159 | { |
paul@252 | 160 | /* Forbid class-relative assignments. */ |
paul@252 | 161 | |
paul@252 | 162 | __raise_type_error(); |
paul@252 | 163 | return 0; |
paul@252 | 164 | } |
paul@252 | 165 | |
paul@126 | 166 | int __check_and_store_via_object(__ref obj, int pos, int code, __attr value) |
paul@126 | 167 | { |
paul@126 | 168 | if (__HASATTR(obj, pos, code)) |
paul@126 | 169 | { |
paul@126 | 170 | __store_via_object(obj, pos, value); |
paul@126 | 171 | return 1; |
paul@126 | 172 | } |
paul@252 | 173 | |
paul@252 | 174 | /* No suitable attribute. */ |
paul@252 | 175 | |
paul@252 | 176 | __raise_type_error(); |
paul@126 | 177 | return 0; |
paul@126 | 178 | } |
paul@126 | 179 | |
paul@126 | 180 | int __check_and_store_via_any(__ref obj, int pos, int code, __attr value) |
paul@126 | 181 | { |
paul@126 | 182 | if (__check_and_store_via_object(obj, pos, code, value)) |
paul@126 | 183 | return 1; |
paul@252 | 184 | |
paul@252 | 185 | /* Forbid class-relative assignments. */ |
paul@252 | 186 | |
paul@252 | 187 | __raise_type_error(); |
paul@252 | 188 | return 0; |
paul@126 | 189 | } |
paul@126 | 190 | |
paul@126 | 191 | /* Context-related operations. */ |
paul@126 | 192 | |
paul@126 | 193 | __attr __test_context(__ref context, __attr attr) |
paul@126 | 194 | { |
paul@267 | 195 | /* Preserve any existing null or instance context. */ |
paul@230 | 196 | |
paul@267 | 197 | if ((attr.context == 0) || __is_instance(attr.context)) |
paul@126 | 198 | return attr; |
paul@235 | 199 | |
paul@235 | 200 | /* Test any instance context against the context employed by the |
paul@235 | 201 | attribute. */ |
paul@126 | 202 | |
paul@235 | 203 | if (__is_instance(context)) |
paul@477 | 204 | { |
paul@235 | 205 | if (__test_common_instance(context, __TYPEPOS(attr.context), __TYPECODE(attr.context))) |
paul@523 | 206 | return __update_context(context, attr); |
paul@235 | 207 | else |
paul@235 | 208 | __raise_type_error(); |
paul@477 | 209 | } |
paul@235 | 210 | |
paul@274 | 211 | /* Test for access to a type class attribute using a type instance. */ |
paul@274 | 212 | |
paul@274 | 213 | if (__test_specific_type(attr.context, &__TYPE_CLASS_TYPE) && __is_type_instance(context)) |
paul@523 | 214 | return __update_context(context, attr); |
paul@274 | 215 | |
paul@235 | 216 | /* Otherwise, preserve the attribute as retrieved. */ |
paul@235 | 217 | |
paul@235 | 218 | return attr; |
paul@126 | 219 | } |
paul@126 | 220 | |
paul@126 | 221 | __attr __update_context(__ref context, __attr attr) |
paul@126 | 222 | { |
paul@521 | 223 | __attr out = {.context=context, .value=attr.value}; |
paul@126 | 224 | return out; |
paul@126 | 225 | } |
paul@126 | 226 | |
paul@523 | 227 | /* Context testing for invocations. */ |
paul@523 | 228 | |
paul@523 | 229 | int __type_method_invocation(__attr attr) |
paul@523 | 230 | { |
paul@523 | 231 | __attr parent; |
paul@523 | 232 | |
paul@523 | 233 | /* Require instances, not classes, where methods are function instances. */ |
paul@523 | 234 | |
paul@523 | 235 | if (!__is_instance(attr.value)) |
paul@523 | 236 | return 0; |
paul@523 | 237 | |
paul@523 | 238 | /* Access the parent of the callable and test if it is the type object. */ |
paul@523 | 239 | |
paul@523 | 240 | parent = __check_and_load_via_object_null(attr.value, __ATTRPOS(__parent__), __ATTRCODE(__parent__)); |
paul@523 | 241 | return ((parent.value != 0) && __test_specific_type(parent.value, &__TYPE_CLASS_TYPE) && __is_type_instance(attr.context)); |
paul@523 | 242 | } |
paul@523 | 243 | |
paul@523 | 244 | __attr (*__get_function(__attr attr))(__attr[]) |
paul@523 | 245 | { |
paul@523 | 246 | /* Require null or instance contexts for functions and methods respectively, |
paul@523 | 247 | or type instance contexts for type methods. */ |
paul@523 | 248 | |
paul@523 | 249 | if ((attr.context == 0) || __is_instance(attr.context) || __type_method_invocation(attr)) |
paul@523 | 250 | return __load_via_object(attr.value, __ATTRPOS(__fn__)).fn; |
paul@523 | 251 | else |
paul@523 | 252 | return __load_via_object(attr.value, __ATTRPOS(__fn__)).inv; |
paul@523 | 253 | } |
paul@523 | 254 | |
paul@523 | 255 | __attr (*__check_and_get_function(__attr attr))(__attr[]) |
paul@523 | 256 | { |
paul@523 | 257 | /* Require null or instance contexts for functions and methods respectively, |
paul@523 | 258 | or type instance contexts for type methods. */ |
paul@523 | 259 | |
paul@523 | 260 | if ((attr.context == 0) || __is_instance(attr.context) || __type_method_invocation(attr)) |
paul@523 | 261 | return __check_and_load_via_object(attr.value, __ATTRPOS(__fn__), __ATTRCODE(__fn__)).fn; |
paul@523 | 262 | else |
paul@523 | 263 | return __check_and_load_via_object(attr.value, __ATTRPOS(__fn__), __ATTRCODE(__fn__)).inv; |
paul@523 | 264 | } |
paul@523 | 265 | |
paul@126 | 266 | /* Basic structure tests. */ |
paul@126 | 267 | |
paul@126 | 268 | int __WITHIN(__ref obj, int pos) |
paul@126 | 269 | { |
paul@126 | 270 | return pos < obj->table->size; |
paul@126 | 271 | } |
paul@126 | 272 | |
paul@126 | 273 | int __HASATTR(__ref obj, int pos, int code) |
paul@126 | 274 | { |
paul@126 | 275 | return __WITHIN(obj, pos) && (obj->table->attrs[pos] == code); |
paul@126 | 276 | } |
paul@126 | 277 | |
paul@126 | 278 | /* Parameter position operations. */ |
paul@126 | 279 | |
paul@126 | 280 | int __HASPARAM(const __ptable *ptable, int ppos, int pcode) |
paul@126 | 281 | { |
paul@126 | 282 | __param param; |
paul@126 | 283 | |
paul@126 | 284 | if (ppos < ptable->size) |
paul@126 | 285 | { |
paul@126 | 286 | param = ptable->params[ppos]; |
paul@126 | 287 | if (param.code == pcode) |
paul@126 | 288 | return param.pos; |
paul@126 | 289 | } |
paul@126 | 290 | |
paul@126 | 291 | return -1; |
paul@126 | 292 | } |
paul@126 | 293 | |
paul@126 | 294 | /* Conversions. */ |
paul@126 | 295 | |
paul@126 | 296 | __attr __CONTEXT_AS_VALUE(__attr attr) |
paul@126 | 297 | { |
paul@126 | 298 | __attr out; |
paul@126 | 299 | out.context = attr.context; |
paul@126 | 300 | out.value = attr.context; |
paul@126 | 301 | return out; |
paul@126 | 302 | } |
paul@126 | 303 | |
paul@126 | 304 | /* Type testing. */ |
paul@126 | 305 | |
paul@144 | 306 | __ref __ISFUNC(__ref obj) |
paul@126 | 307 | { |
paul@126 | 308 | return __test_specific_instance(obj, &__FUNCTION_TYPE); |
paul@126 | 309 | } |
paul@126 | 310 | |
paul@126 | 311 | int __ISNULL(__attr value) |
paul@126 | 312 | { |
paul@143 | 313 | /* (value.context == __NULL.context) is superfluous */ |
paul@143 | 314 | return (value.value == 0); /* __NULL.value */ |
paul@126 | 315 | } |
paul@126 | 316 | |
paul@126 | 317 | /* Attribute codes and positions for type objects. */ |
paul@126 | 318 | |
paul@126 | 319 | unsigned int __TYPECODE(__ref obj) |
paul@126 | 320 | { |
paul@126 | 321 | return obj->table->attrs[obj->pos]; |
paul@126 | 322 | } |
paul@126 | 323 | |
paul@126 | 324 | unsigned int __TYPEPOS(__ref obj) |
paul@126 | 325 | { |
paul@126 | 326 | return obj->pos; |
paul@126 | 327 | } |
paul@151 | 328 | |
paul@260 | 329 | /* Memory allocation. */ |
paul@260 | 330 | |
paul@260 | 331 | void *__ALLOCATE(size_t nmemb, size_t size) |
paul@260 | 332 | { |
paul@433 | 333 | void *ptr = GC_MALLOC(nmemb * size); /* sets memory to zero */ |
paul@260 | 334 | if (ptr == NULL) |
paul@260 | 335 | __raise_memory_error(); |
paul@260 | 336 | return ptr; |
paul@260 | 337 | } |
paul@260 | 338 | |
paul@260 | 339 | void *__REALLOCATE(void *ptr, size_t size) |
paul@260 | 340 | { |
paul@433 | 341 | void *nptr = GC_REALLOC(ptr, size); |
paul@260 | 342 | if (nptr == NULL) |
paul@260 | 343 | __raise_memory_error(); |
paul@260 | 344 | return nptr; |
paul@260 | 345 | } |
paul@260 | 346 | |
paul@151 | 347 | /* Copying of structures. */ |
paul@151 | 348 | |
paul@151 | 349 | __ref __COPY(__ref obj, int size) |
paul@151 | 350 | { |
paul@260 | 351 | __ref copy = (__ref) __ALLOCATE(1, size); |
paul@151 | 352 | memcpy(copy, obj, size); |
paul@151 | 353 | return copy; |
paul@151 | 354 | } |