Lichen

Annotated templates/ops.h

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