Lichen

templates/ops.h

639:93254c89358b
2017-02-28 Paul Boddie Added testing of temporary result variable availability.
     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 /* Direct access and manipulation of static objects. */    26     27 __attr __load_static_ignore(__ref obj);    28 __attr __load_static_replace(__ref context, __ref obj);    29 __attr __load_static_test(__ref context, __ref obj);    30     31 /* Direct retrieval operations, returning attributes. */    32     33 __attr __load_via_class__(__ref obj, int pos);    34 __attr __load_via_object__(__ref obj, int pos);    35 __attr __get_class_and_load__(__ref obj, int pos);    36     37 #define __load_via_class(OBJ, ATTRNAME) (__load_via_class__(OBJ, __ATTRPOS(ATTRNAME)))    38 #define __load_via_object(OBJ, ATTRNAME) (__load_via_object__(OBJ, __ATTRPOS(ATTRNAME)))    39 #define __get_class_and_load(OBJ, ATTRNAME) (__get_class_and_load__(OBJ, __ATTRPOS(ATTRNAME)))    40     41 /* Direct storage operations. */    42     43 int __store_via_object__(__ref obj, int pos, __attr value);    44 int __get_class_and_store__(__ref obj, int pos, __attr value);    45     46 #define __store_via_object(OBJ, ATTRNAME, VALUE) (__store_via_object__(OBJ, __ATTRPOS(ATTRNAME), VALUE))    47 #define __get_class_and_store(OBJ, ATTRNAME, VALUE) (__get_class_and_store__(OBJ, __ATTRPOS(ATTRNAME), VALUE))    48     49 /* Introspection. */    50     51 int __is_instance(__ref obj);    52 int __is_type_instance(__ref obj);    53 __ref __get_class(__ref obj);    54 __attr __get_class_attr(__ref obj);    55     56 /* Attribute testing operations. */    57     58 __ref __test_specific_instance(__ref obj, __ref type);    59 __ref __test_specific_object(__ref obj, __ref type);    60 __ref __test_specific_type(__ref obj, __ref type);    61     62 __ref __test_common_instance__(__ref obj, int pos, int code);    63 __ref __test_common_object__(__ref obj, int pos, int code);    64 __ref __test_common_type__(__ref obj, int pos, int code);    65     66 #define __test_common_instance(OBJ, TYPENAME) (__test_common_instance__(OBJ, __ATTRPOS(TYPENAME), __ATTRCODE(TYPENAME)))    67 #define __test_common_object(OBJ, TYPENAME) (__test_common_object__(OBJ, __ATTRPOS(TYPENAME), __ATTRCODE(TYPENAME)))    68 #define __test_common_type(OBJ, TYPENAME) (__test_common_type__(OBJ, __ATTRPOS(TYPENAME), __ATTRCODE(TYPENAME)))    69     70 /* Attribute testing and retrieval operations. */    71     72 __attr __check_and_load_via_object_null(__ref obj, int pos, int code);    73     74 __attr __check_and_load_via_class__(__ref obj, int pos, int code);    75 __attr __check_and_load_via_object__(__ref obj, int pos, int code);    76 __attr __check_and_load_via_any__(__ref obj, int pos, int code);    77     78 #define __check_and_load_via_class(OBJ, ATTRNAME) (__check_and_load_via_class__(OBJ, __ATTRPOS(ATTRNAME), __ATTRCODE(ATTRNAME)))    79 #define __check_and_load_via_object(OBJ, ATTRNAME) (__check_and_load_via_object__(OBJ, __ATTRPOS(ATTRNAME), __ATTRCODE(ATTRNAME)))    80 #define __check_and_load_via_any(OBJ, ATTRNAME) (__check_and_load_via_any__(OBJ, __ATTRPOS(ATTRNAME), __ATTRCODE(ATTRNAME)))    81     82 /* Attribute testing and storage operations. */    83     84 int __check_and_store_via_class__(__ref obj, int pos, int code, __attr value);    85 int __check_and_store_via_object__(__ref obj, int pos, int code, __attr value);    86 int __check_and_store_via_any__(__ref obj, int pos, int code, __attr value);    87     88 #define __check_and_store_via_class(OBJ, ATTRNAME, VALUE) (__check_and_store_via_class__(OBJ, __ATTRPOS(ATTRNAME), __ATTRCODE(ATTRNAME), VALUE))    89 #define __check_and_store_via_object(OBJ, ATTRNAME, VALUE) (__check_and_store_via_object__(OBJ, __ATTRPOS(ATTRNAME), __ATTRCODE(ATTRNAME), VALUE))    90 #define __check_and_store_via_any(OBJ, ATTRNAME, VALUE) (__check_and_store_via_any__(OBJ, __ATTRPOS(ATTRNAME), __ATTRCODE(ATTRNAME), VALUE))    91     92 /* Context-related operations. */    93     94 int __test_context_update(__ref context, __attr attr);    95 __attr __test_context(__ref context, __attr attr);    96 __attr __update_context(__ref context, __attr attr);    97 __attr __test_context_revert(int target, __ref context, __attr attr, __ref contexts[]);    98 __attr __test_context_static(int target, __ref context, __ref value, __ref contexts[]);    99    100 #define __get_context(__TARGET) (__tmp_contexts[__TARGET])   101 #define __set_context(__TARGET, __ATTR) (__tmp_contexts[__TARGET] = (__ATTR).value)   102 #define __set_private_context(__ATTR) (__tmp_private_context = (__ATTR).value)   103 #define __set_accessor(__ATTR) (__tmp_value = (__ATTR).value)   104 #define __set_target_accessor(__ATTR) (__tmp_target_value = (__ATTR).value)   105    106 /* Context testing for invocations. */   107    108 __attr __unwrap_callable(__attr callable);   109 __attr (*__get_function(__ref context, __attr target))(__attr[]);   110 __attr (*__check_and_get_function(__ref context, __attr target))(__attr[]);   111    112 /* Basic structure tests. */   113    114 int __WITHIN(__ref obj, int pos);   115 int __HASATTR(__ref obj, int pos, int code);   116    117 /* Parameter position operations. */   118    119 int __HASPARAM(const __ptable *ptable, int ppos, int pcode);   120    121 /* Conversions. */   122    123 __attr __CONTEXT_AS_VALUE(__attr attr);   124    125 /* Type testing. */   126    127 __ref __ISFUNC(__ref obj);   128 int __ISNULL(__attr value);   129    130 /* __TEST(obj, __A) -> test obj for the special type attribute __A */   131    132 #define __TEST(__OBJ, __TYPE) (__test_common_instance(__OBJ, __pos_##__TYPE, __code_##__TYPE))   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 *__REALLOCATE(void *ptr, size_t size);   143    144 /* Copying of structures. */   145    146 __ref __COPY(__ref obj, int size);   147    148 #endif /* __OPS_H__ */