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