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