1 /* Common operations. 2 3 Copyright (C) 2015, 2016, 2017, 2018 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 #ifndef __OPS_H__ 20 #define __OPS_H__ 21 22 #include "types.h" 23 #include <string.h> /* for __COPY */ 24 25 /* Get object reference from attribute. */ 26 27 __ref __VALUE(__attr attr); 28 29 /* Direct access and manipulation of static objects. */ 30 31 __attr __load_static_ignore(__ref obj); 32 __attr __load_static_replace(__attr context, __ref obj); 33 __attr __load_static_test(__attr context, __ref obj); 34 35 /* Direct retrieval operations, returning attributes. */ 36 37 __attr __load_via_class__(__ref obj, int pos); 38 __attr __load_via_object__(__ref obj, int pos); 39 __attr __get_class_and_load__(__ref obj, int pos); 40 41 #define __load_via_class(OBJ, ATTRNAME) (__load_via_class__(OBJ, __ATTRPOS(ATTRNAME))) 42 #define __load_via_object(OBJ, ATTRNAME) (__load_via_object__(OBJ, __ATTRPOS(ATTRNAME))) 43 #define __get_class_and_load(OBJ, ATTRNAME) (__get_class_and_load__(OBJ, __ATTRPOS(ATTRNAME))) 44 45 /* Direct storage operations. */ 46 47 int __store_via_class__(__ref obj, int pos, __attr value); 48 int __store_via_object__(__ref obj, int pos, __attr value); 49 int __get_class_and_store__(__ref obj, int pos, __attr value); 50 51 #define __store_via_class(OBJ, ATTRNAME, VALUE) (__store_via_class__(OBJ, __ATTRPOS(ATTRNAME), VALUE)) 52 #define __store_via_object(OBJ, ATTRNAME, VALUE) (__store_via_object__(OBJ, __ATTRPOS(ATTRNAME), VALUE)) 53 #define __get_class_and_store(OBJ, ATTRNAME, VALUE) (__get_class_and_store__(OBJ, __ATTRPOS(ATTRNAME), VALUE)) 54 55 /* Introspection. */ 56 57 int __is_instance(__ref obj); 58 int __is_subclass(__ref obj, __attr cls); 59 int __is_instance_subclass(__ref obj, __attr cls); 60 int __is_type_instance(__ref obj); 61 __ref __get_class(__ref obj); 62 __attr __get_class_attr(__ref obj); 63 64 /* Attribute testing operations. */ 65 66 __ref __test_specific_instance(__ref obj, __ref type); 67 __ref __test_specific_object(__ref obj, __ref type); 68 __ref __test_specific_type(__ref obj, __ref type); 69 70 __ref __test_common_instance__(__ref obj, int pos, int code); 71 __ref __test_common_object__(__ref obj, int pos, int code); 72 __ref __test_common_type__(__ref obj, int pos, int code); 73 74 #define __to_error(REF) (REF ? REF : (__raise_type_error(), (__ref) 0)) 75 76 #define __test_common_instance(OBJ, TYPENAME) (__test_common_instance__(OBJ, __ATTRPOS(TYPENAME), __ATTRCODE(TYPENAME))) 77 #define __test_common_object(OBJ, TYPENAME) (__test_common_object__(OBJ, __ATTRPOS(TYPENAME), __ATTRCODE(TYPENAME))) 78 #define __test_common_type(OBJ, TYPENAME) (__test_common_type__(OBJ, __ATTRPOS(TYPENAME), __ATTRCODE(TYPENAME))) 79 80 /* Attribute testing and retrieval operations. */ 81 82 __attr __check_and_load_via_object_null(__ref obj, int pos, int code); 83 84 __attr __check_and_load_via_class__(__ref obj, int pos, int code); 85 __attr __check_and_load_via_object__(__ref obj, int pos, int code); 86 __attr __check_and_load_via_any__(__ref obj, int pos, int code); 87 88 #define __check_and_load_via_class(OBJ, ATTRNAME) (__check_and_load_via_class__(OBJ, __ATTRPOS(ATTRNAME), __ATTRCODE(ATTRNAME))) 89 #define __check_and_load_via_object(OBJ, ATTRNAME) (__check_and_load_via_object__(OBJ, __ATTRPOS(ATTRNAME), __ATTRCODE(ATTRNAME))) 90 #define __check_and_load_via_any(OBJ, ATTRNAME) (__check_and_load_via_any__(OBJ, __ATTRPOS(ATTRNAME), __ATTRCODE(ATTRNAME))) 91 92 /* Attribute testing and storage operations. */ 93 94 int __check_and_store_via_class__(__ref obj, int pos, int code, __attr value); 95 int __check_and_store_via_object__(__ref obj, int pos, int code, __attr value); 96 int __check_and_store_via_any__(__ref obj, int pos, int code, __attr value); 97 98 #define __check_and_store_via_class(OBJ, ATTRNAME, VALUE) (__check_and_store_via_class__(OBJ, __ATTRPOS(ATTRNAME), __ATTRCODE(ATTRNAME), VALUE)) 99 #define __check_and_store_via_object(OBJ, ATTRNAME, VALUE) (__check_and_store_via_object__(OBJ, __ATTRPOS(ATTRNAME), __ATTRCODE(ATTRNAME), VALUE)) 100 #define __check_and_store_via_any(OBJ, ATTRNAME, VALUE) (__check_and_store_via_any__(OBJ, __ATTRPOS(ATTRNAME), __ATTRCODE(ATTRNAME), VALUE)) 101 102 /* Context-related operations. */ 103 104 int __test_context_update(__attr context, __attr attr, int invoke); 105 __attr __test_context(__attr context, __attr attr); 106 __attr __update_context(__attr context, __attr attr); 107 __attr __test_context_revert(int target, __attr context, __attr attr, __attr contexts[]); 108 __attr __test_context_static(int target, __attr context, __ref value, __attr contexts[]); 109 110 #define __get_accessor(__TARGET) (__tmp_values[__TARGET]) 111 #define __get_context(__TARGET) (__tmp_contexts[__TARGET]) 112 #define __set_context(__TARGET, __ATTR) (__tmp_contexts[__TARGET] = (__ATTR)) 113 #define __set_private_context(__ATTR) (__tmp_private_context = (__ATTR)) 114 #define __set_accessor(__TARGET, __ATTR) (__tmp_values[__TARGET] = (__ATTR)) 115 #define __set_target_accessor(__ATTR) (__tmp_target_value = (__ATTR)) 116 117 /* Context testing for invocations. */ 118 119 __attr __unwrap_callable(__attr callable); 120 __attr (*__get_function_unchecked(__attr target))(); 121 __attr (*__get_function(__attr context, __attr target))(); 122 __attr (*__get_function_unwrapped(__attr context, __attr target))(); 123 __attr (*__get_function_member(__attr target))(); 124 __attr (*__check_and_get_function(__attr context, __attr target))(); 125 __attr (*__check_and_get_function_unwrapped(__attr context, __attr target))(); 126 127 /* Parameter position operations. */ 128 129 int __HASPARAM(const __ptable *ptable, int ppos, int pcode); 130 131 /* Conversions. */ 132 133 __attr __CONTEXT_AS_VALUE(__attr attr); 134 135 /* Type testing. */ 136 137 __ref __ISFUNC(__ref obj); 138 139 #define __ISNULL(__ATTR) (!__ATTR.value) 140 141 /* Attribute codes and positions for type objects. */ 142 143 unsigned int __TYPECODE(__ref obj); 144 unsigned int __TYPEPOS(__ref obj); 145 146 /* Memory allocation. */ 147 148 void *__ALLOCATE(size_t nmemb, size_t size); 149 void *__ALLOCATEIM(size_t nmemb, size_t size); 150 void *__REALLOCATE(void *ptr, size_t size); 151 152 /* Copying of structures. */ 153 154 __ref __COPY(__ref obj, int size); 155 156 #endif /* __OPS_H__ */