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