1 /* Common operations. 2 3 Copyright (C) 2015, 2016, 2017, 2018, 2019 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 /* Object state updating when assigning. */ 46 47 __attr __store_attr__(__attr value); 48 49 /* Direct storage operations. */ 50 51 int __store_member__(__ref obj, int pos, __attr value); 52 53 #define __store_local(NAME, VALUE) (NAME = __store_attr__(VALUE)) 54 /* #define __store_local(NAME, VALUE) (NAME = VALUE) */ 55 #define __store_member(OBJ, ATTRNAME, VALUE) (__store_member__(OBJ, __ATTRPOS(ATTRNAME), VALUE)) 56 57 int __store_via_class__(__ref obj, int pos, __attr value); 58 int __store_via_object__(__ref obj, int pos, __attr value); 59 int __get_class_and_store__(__ref obj, int pos, __attr value); 60 61 #define __store_via_class(OBJ, ATTRNAME, VALUE) (__store_via_class__(OBJ, __ATTRPOS(ATTRNAME), VALUE)) 62 #define __store_via_object(OBJ, ATTRNAME, VALUE) (__store_via_object__(OBJ, __ATTRPOS(ATTRNAME), VALUE)) 63 #define __get_class_and_store(OBJ, ATTRNAME, VALUE) (__get_class_and_store__(OBJ, __ATTRPOS(ATTRNAME), VALUE)) 64 65 /* Introspection. */ 66 67 int __is_instance(__ref obj); 68 int __is_subclass(__ref obj, __attr cls); 69 int __is_instance_subclass(__ref obj, __attr cls); 70 int __is_type_instance(__ref obj); 71 __ref __get_class(__ref obj); 72 __attr __get_class_attr(__ref obj); 73 74 /* Attribute testing operations. */ 75 76 __ref __test_specific_instance(__ref obj, __ref type); 77 __ref __test_specific_object(__ref obj, __ref type); 78 __ref __test_specific_type(__ref obj, __ref type); 79 80 __ref __test_common_instance__(__ref obj, int pos, int code); 81 __ref __test_common_object__(__ref obj, int pos, int code); 82 __ref __test_common_type__(__ref obj, int pos, int code); 83 84 #define __to_error(REF) (REF ? REF : (__raise_type_error(), (__ref) 0)) 85 86 #define __test_common_instance(OBJ, TYPENAME) (__test_common_instance__(OBJ, __ATTRPOS(TYPENAME), __ATTRCODE(TYPENAME))) 87 #define __test_common_object(OBJ, TYPENAME) (__test_common_object__(OBJ, __ATTRPOS(TYPENAME), __ATTRCODE(TYPENAME))) 88 #define __test_common_type(OBJ, TYPENAME) (__test_common_type__(OBJ, __ATTRPOS(TYPENAME), __ATTRCODE(TYPENAME))) 89 90 /* Attribute testing and retrieval operations. */ 91 92 __attr __check_and_load_via_object_null(__ref obj, int pos, int code); 93 94 __attr __check_and_load_via_class__(__ref obj, int pos, int code); 95 __attr __check_and_load_via_object__(__ref obj, int pos, int code); 96 __attr __check_and_load_via_any__(__ref obj, int pos, int code); 97 98 #define __check_and_load_via_class(OBJ, ATTRNAME) (__check_and_load_via_class__(OBJ, __ATTRPOS(ATTRNAME), __ATTRCODE(ATTRNAME))) 99 #define __check_and_load_via_object(OBJ, ATTRNAME) (__check_and_load_via_object__(OBJ, __ATTRPOS(ATTRNAME), __ATTRCODE(ATTRNAME))) 100 #define __check_and_load_via_any(OBJ, ATTRNAME) (__check_and_load_via_any__(OBJ, __ATTRPOS(ATTRNAME), __ATTRCODE(ATTRNAME))) 101 102 /* Attribute testing and storage operations. */ 103 104 int __check_and_store_via_class__(__ref obj, int pos, int code, __attr value); 105 int __check_and_store_via_object__(__ref obj, int pos, int code, __attr value); 106 int __check_and_store_via_any__(__ref obj, int pos, int code, __attr value); 107 108 #define __check_and_store_via_class(OBJ, ATTRNAME, VALUE) (__check_and_store_via_class__(OBJ, __ATTRPOS(ATTRNAME), __ATTRCODE(ATTRNAME), VALUE)) 109 #define __check_and_store_via_object(OBJ, ATTRNAME, VALUE) (__check_and_store_via_object__(OBJ, __ATTRPOS(ATTRNAME), __ATTRCODE(ATTRNAME), VALUE)) 110 #define __check_and_store_via_any(OBJ, ATTRNAME, VALUE) (__check_and_store_via_any__(OBJ, __ATTRPOS(ATTRNAME), __ATTRCODE(ATTRNAME), VALUE)) 111 112 /* Context-related operations. */ 113 114 int __test_context_update(__attr context, __attr attr, int invoke); 115 __attr __test_context(__attr context, __attr attr); 116 __attr __update_context(__attr context, __attr attr); 117 __attr __test_context_revert(int target, __attr context, __attr attr, __attr contexts[]); 118 __attr __test_context_static(int target, __attr context, __ref value, __attr contexts[]); 119 120 #define __get_accessor(__TARGET) (__tmp_values[__TARGET]) 121 #define __get_context(__TARGET) (__tmp_contexts[__TARGET]) 122 #define __set_context(__TARGET, __ATTR) (__tmp_contexts[__TARGET] = (__ATTR)) 123 #define __set_private_context(__ATTR) (__tmp_private_context = (__ATTR)) 124 #define __set_accessor(__TARGET, __ATTR) (__tmp_values[__TARGET] = (__ATTR)) 125 #define __set_target_accessor(__ATTR) (__tmp_target_value = (__ATTR)) 126 127 /* Context testing for invocations. */ 128 129 __attr __unwrap_callable(__attr callable); 130 __attr (*__get_function_unchecked(__attr target))(); 131 __attr (*__get_function(__attr context, __attr target))(); 132 __attr (*__get_function_unwrapped(__attr context, __attr target))(); 133 __attr (*__get_function_member(__attr target))(); 134 __attr (*__check_and_get_function(__attr context, __attr target))(); 135 __attr (*__check_and_get_function_unwrapped(__attr context, __attr target))(); 136 137 /* Parameter position operations. */ 138 139 int __HASPARAM(const __ptable *ptable, int ppos, int pcode); 140 141 /* Conversions. */ 142 143 __attr __CONTEXT_AS_VALUE(__attr attr); 144 145 /* Type testing. */ 146 147 __ref __ISFUNC(__ref obj); 148 149 #define __ISNULL(__ATTR) (!__ATTR.value) 150 151 /* Attribute codes and positions for type objects. */ 152 153 unsigned int __TYPECODE(__ref obj); 154 unsigned int __TYPEPOS(__ref obj); 155 156 /* Memory allocation. */ 157 158 void *__ALLOCATE(size_t nmemb, size_t size); 159 void *__ALLOCATEIM(size_t nmemb, size_t size); 160 void *__REALLOCATE(void *ptr, size_t size); 161 162 /* Copying of structures. */ 163 164 __ref __COPY(__ref obj, int size); 165 166 #endif /* __OPS_H__ */