1 /* Runtime types. 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 __TYPES_H__ 20 #define __TYPES_H__ 21 22 #include <stddef.h> /* size_t */ 23 24 /* Attribute tables are lists of codes confirming the presence of attributes. */ 25 26 typedef struct __table 27 { 28 const unsigned int size, attrs[]; 29 } __table; 30 31 /* Parameter tables are lists of codes confirming the presence of parameters, as 32 well as the positions of those parameters in the list for a given function. 33 */ 34 35 typedef struct __param 36 { 37 unsigned short code, pos; 38 } __param; 39 40 typedef struct __ptable 41 { 42 const unsigned int size; 43 const __param params[]; 44 } __ptable; 45 46 /* Attributes are context and value pairs. 47 Objects are collections of attributes. 48 Object references are references to tables and collections of attributes. 49 Attribute references are references to single attributes. */ 50 51 typedef struct __obj __obj; 52 typedef struct __fragment __fragment; 53 54 typedef struct __attr 55 { 56 /* One of... */ 57 union 58 { 59 struct { 60 __obj * context; /* attribute context */ 61 __obj * value; /* attribute value */ 62 }; 63 struct { 64 unsigned int min; /* minimum number of parameters */ 65 const __ptable * ptable; /* parameter table */ 66 }; 67 struct { 68 unsigned int code; /* parameter table code for key */ 69 unsigned int pos; /* parameter table position for key */ 70 }; 71 struct { 72 struct __attr (*inv)(); /* unbound callable details */ 73 struct __attr (*fn)(); /* callable details */ 74 }; 75 struct { 76 size_t size; /* size of value */ 77 union 78 { 79 int intvalue; /* integer value */ 80 double floatvalue; /* floating point value */ 81 char * strvalue; /* string value */ 82 __fragment * seqvalue; /* sequence data */ 83 void * datavalue; /* object-specific data */ 84 }; 85 }; 86 }; 87 } __attr; 88 89 typedef struct __obj 90 { 91 const __table * table; /* attribute table */ 92 unsigned int pos; /* position of attribute indicating class */ 93 __attr attrs[]; /* attributes */ 94 } __obj; 95 96 typedef __obj * __ref; 97 98 /* Fragments are simple collections of attributes employed by sequence types. 99 They provide the basis of lists and tuples. */ 100 101 typedef struct __fragment 102 { 103 unsigned int size, capacity; 104 __attr attrs[]; 105 } __fragment; 106 107 #define __FRAGMENT_SIZE(NUMBER) (NUMBER * sizeof(__attr) + 2 * sizeof(unsigned int)) 108 109 /* Special instance position value. The pos member of __obj refers to the 110 special type attribute for classes, indicating which position holds the 111 attribute describing the class type. For instances, it is set to zero. */ 112 113 #define __INSTANCEPOS 0 114 115 /* Special null values. */ 116 117 #define __NULL ((__attr) {{.context=0, .value=0}}) 118 119 /* Function pointer type. */ 120 121 typedef __attr (*__func)(); 122 123 /* Convenience macros. */ 124 125 #define __ARGS(...) ((__attr[]) {__VA_ARGS__}) 126 #define __KWARGS(...) ((__param[]) {__VA_ARGS__}) 127 128 #endif /* __TYPES_H__ */