1 /* Common operations. 2 3 Copyright (C) 2015, 2016, 2017, 2018, 2023 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, __attr context, __ref obj); 33 __attr __load_static_test(__attr __context, __attr context, __ref obj); 34 35 /* Direct retrieval operations, returning attribute locations. */ 36 37 __attr *__get_class_attr_ref__(__ref obj, int pos); 38 39 #define __get_object_attr_ref__(OBJ, POS) (&(OBJ)->attrs[POS]) 40 41 #define __get_object_attr_ref(OBJ, ATTRNAME) (__get_object_attr_ref__(OBJ, __ATTRPOS(ATTRNAME))) 42 #define __get_class_attr_ref(OBJ, ATTRNAME) (__get_class_attr_ref__(OBJ, __ATTRPOS(ATTRNAME))) 43 44 /* Attribute location operations. */ 45 46 #define __load_via_attr_ref(ATTR) (*(ATTR)) 47 #define __store_via_attr_ref(ATTR, VALUE) __set_attr(ATTR, VALUE) 48 49 #define __store_via_attr_ref__(ATTR, VALUE) (*(ATTR) = VALUE) 50 51 /* Direct retrieval operations, returning attributes. */ 52 53 __attr __load_via_class__(__ref obj, int pos); 54 __attr __load_via_object__(__ref obj, int pos); 55 __attr __get_class_and_load__(__ref obj, int pos); 56 57 #define __load_via_class(OBJ, ATTRNAME) (__load_via_class__(OBJ, __ATTRPOS(ATTRNAME))) 58 #define __load_via_object(OBJ, ATTRNAME) (__load_via_object__(OBJ, __ATTRPOS(ATTRNAME))) 59 #define __get_class_and_load(OBJ, ATTRNAME) (__get_class_and_load__(OBJ, __ATTRPOS(ATTRNAME))) 60 61 /* Introspection. */ 62 63 int __is_instance(__ref obj); 64 int __is_subclass(__ref obj, __attr cls); 65 int __is_instance_subclass(__ref obj, __attr cls); 66 int __is_type_instance(__ref obj); 67 __ref __get_class(__ref obj); 68 __attr __get_class_attr(__ref obj); 69 70 /* Attribute testing operations. */ 71 72 __ref __test_specific_instance(__ref obj, __ref type); 73 __ref __test_specific_object(__ref obj, __ref type); 74 __ref __test_specific_type(__ref obj, __ref type); 75 76 __ref __test_common_instance__(__ref obj, int pos, int code); 77 __ref __test_common_object__(__ref obj, int pos, int code); 78 __ref __test_common_type__(__ref obj, int pos, int code); 79 80 #define __to_error(REF) (REF ? REF : (__raise_type_error(), (__ref) 0)) 81 82 #define __test_common_instance(OBJ, TYPENAME) (__test_common_instance__(OBJ, __ATTRPOS(TYPENAME), __ATTRCODE(TYPENAME))) 83 #define __test_common_object(OBJ, TYPENAME) (__test_common_object__(OBJ, __ATTRPOS(TYPENAME), __ATTRCODE(TYPENAME))) 84 #define __test_common_type(OBJ, TYPENAME) (__test_common_type__(OBJ, __ATTRPOS(TYPENAME), __ATTRCODE(TYPENAME))) 85 86 /* Attribute testing and location operations, returning the address of the 87 attribute as opposed to its value. */ 88 89 __attr *__check_and_get_object_attr_ref_null(__ref obj, int pos, int code); 90 91 __attr *__check_and_get_object_attr_ref__(__ref obj, int pos, int code); 92 93 #define __check_and_get_object_attr_ref(OBJ, ATTRNAME) (__check_and_get_object_attr_ref__(OBJ, __ATTRPOS(ATTRNAME), __ATTRCODE(ATTRNAME))) 94 95 /* Attribute testing and retrieval operations. */ 96 97 __attr __check_and_load_via_object_null(__ref obj, int pos, int code); 98 99 __attr __check_and_load_via_class__(__ref obj, int pos, int code); 100 __attr __check_and_load_via_object__(__ref obj, int pos, int code); 101 __attr __check_and_load_via_any__(__ref obj, int pos, int code); 102 103 #define __check_and_load_via_class(OBJ, ATTRNAME) (__check_and_load_via_class__(OBJ, __ATTRPOS(ATTRNAME), __ATTRCODE(ATTRNAME))) 104 #define __check_and_load_via_object(OBJ, ATTRNAME) (__check_and_load_via_object__(OBJ, __ATTRPOS(ATTRNAME), __ATTRCODE(ATTRNAME))) 105 #define __check_and_load_via_any(OBJ, ATTRNAME) (__check_and_load_via_any__(OBJ, __ATTRPOS(ATTRNAME), __ATTRCODE(ATTRNAME))) 106 107 /* Context-related operations. */ 108 109 int __test_context_update(__attr context, __attr attr, int invoke); 110 __attr __test_context_revert(int target, __attr context, __attr attr, __attr contexts[]); 111 __attr __test_context_static(int target, __attr context, __ref value, __attr contexts[]); 112 113 __attr __test_context(__attr __context, __attr context, __attr attr); 114 __attr __update_context(__attr __context, __attr context, __attr attr); 115 116 #define __get_accessor(__TARGET) (__tmp_values[__TARGET]) 117 #define __get_attr_ref(__TARGET) (__tmp_attr_refs[__TARGET]) 118 #define __get_context(__TARGET) (__tmp_contexts[__TARGET]) 119 #define __set_context(__TARGET, __ATTR) (__tmp_contexts[__TARGET] = (__ATTR)) 120 #define __set_private_context(__ATTR) (__tmp_private_context = (__ATTR)) 121 #define __set_accessor(__TARGET, __ATTR) (__tmp_values[__TARGET] = (__ATTR)) 122 #define __set_target_accessor(__ATTR) (__tmp_target_value = (__ATTR)) 123 #define __set_attr_ref(__TARGET, __ATTR) (__tmp_attr_refs[__TARGET] = (__ATTR)) 124 125 /* Context testing for invocations. */ 126 127 __attr __unwrap_callable(__attr callable); 128 __attr (*__get_function_unchecked(__attr target))(); 129 __attr (*__get_function(__attr context, __attr target))(); 130 __attr (*__get_function_unwrapped(__attr context, __attr target))(); 131 __attr (*__get_function_member(__attr target))(); 132 __attr (*__check_and_get_function(__attr context, __attr target))(); 133 __attr (*__check_and_get_function_unwrapped(__attr context, __attr target))(); 134 135 /* Parameter position operations. */ 136 137 int __HASPARAM(const __ptable *ptable, int ppos, int pcode); 138 139 /* Conversions. */ 140 141 __attr __CONTEXT_AS_VALUE(__attr attr); 142 143 /* Type testing. */ 144 145 __ref __ISFUNC(__ref obj); 146 147 #define __ISNULL(__ATTR) (!(__ATTR).value) 148 149 /* Attribute codes and positions for type objects. */ 150 151 unsigned int __TYPECODE(__ref obj); 152 unsigned int __TYPEPOS(__ref obj); 153 154 /* Memory allocation. */ 155 156 void *__ALLOCATE(size_t nmemb, size_t size); 157 void *__ALLOCATEIM(size_t nmemb, size_t size); 158 void *__REALLOCATE(void *ptr, size_t size); 159 160 /* Copying of structures. */ 161 162 __ref __COPY(__ref obj, int size); 163 164 /* Result target administration for potential value replacement. */ 165 166 __attr __set_attr(volatile __attr *target, __attr attr); 167 168 #endif /* __OPS_H__ */