Lichen

templates/types.h

1045:e6010ccae0c0
5 months ago Paul Boddie Fixed list element assignment, overlooked in previous value replacement changes. value-replacement
     1 /* Runtime types.     2      3 Copyright (C) 2015-2019, 2021, 2023 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 /* Define code and position types, populated by enum values defined for each    23    program specifically. */    24     25 #include <stdint.h>    26 #include <stdlib.h>    27     28 /* Include the special instance position value. The pos member of __obj refers    29    to the special type attribute for classes, indicating which position holds    30    the attribute describing the class type. For instances, it is set to the same    31    attribute position as __class__ and is defined in the following file. */    32     33 #include "instancepos.h"    34     35 typedef uint16_t __code;    36 typedef uint16_t __pos;    37 typedef uint16_t __pcode;    38 typedef uint16_t __ppos;    39     40 /* Attribute tables are lists of codes confirming the presence of attributes. */    41     42 typedef struct __table    43 {    44     const __pos size;    45     const __code attrs[];    46 } __table;    47     48 /* Parameter tables are lists of codes confirming the presence of parameters, as    49    well as the positions of those parameters in the list for a given function.    50 */    51     52 typedef struct __param    53 {    54     __code code;    55     __pos pos;    56 } __param;    57     58 typedef struct __ptable    59 {    60     const __ppos min, max, size;    61     const __param params[];    62 } __ptable;    63     64 /* Attributes are values referring to objects or encoding other information.    65    Objects are collections of attributes.    66    Object references are references to tables and collections of attributes.    67    Attribute references are references to single attributes. */    68     69 typedef struct __obj __obj;    70 typedef struct __fragment __fragment;    71 typedef union __attr __attr;    72 typedef __obj * __ref;    73     74 /* Introduce an integer type that is interoperable with the size type. */    75     76 typedef ssize_t __int;    77     78 /* Introduce a tagged pointer type. */    79     80 typedef uintptr_t __ref_tagged;    81     82 /* Attribute value interpretations. */    83     84 typedef union __attr    85 {    86     /* General attribute members. */    87     88     __ref value;                /* attribute value */    89     __int intvalue;             /* integer value data or object-specific size    90                                    (shifted value, tagged) */    91     __ref_tagged refvalue;      /* attribute value with tag */    92     93     /* Special case attribute members. */    94     95     const __ptable * ptable;    /* parameter table */    96     struct {    97         __pcode code;           /* parameter table code for key */    98         __ppos pos;             /* parameter table position for key */    99     };   100     __attr (*fn)();             /* callable details */   101     char * strvalue;            /* string value */   102     __fragment * seqvalue;      /* sequence data */   103     void * datavalue;           /* object-specific data */   104 } __attr;   105    106 typedef struct __obj   107 {   108     const __table * table;      /* attribute table */   109     __ppos pos;                 /* position of attribute indicating class */   110     __attr attrs[];             /* attributes */   111    112     /* Specialisations of this type may add other members.   113        See generator.py for type generation, progops.h for definitions, and   114        the generated progtypes.h for the final details. */   115    116 } __obj;   117    118 /* Fragments are simple collections of attributes employed by sequence types.   119    They provide the basis of lists and tuples. */   120    121 typedef struct __fragment   122 {   123     __int size, capacity;   124     __attr attrs[];   125 } __fragment;   126    127 #define __FRAGMENT_SIZE(NUMBER) ((NUMBER) * sizeof(__attr) + 2 * sizeof(__int))   128    129 /* Attribute interpretation. */   130    131 #define __NUM_TAG_BITS      2   132 #define __TAG_INT           0b01   133 #define __TAG_REPLACING     0b10   134 #define __INTEGER(ATTR)     ((ATTR).intvalue & __TAG_INT)   135    136 /* Attribute value setting. */   137    138 #define __ATTRVALUE(VALUE)  ((__attr) {.value=(__ref) VALUE})   139 #define __NULL              __ATTRVALUE(0)   140 #define __SETNULL(ATTR)     ((ATTR).value = 0)   141    142 /* Attribute as instance value. */   143    144 #define __FROMINT(VALUE)    ((((__int) VALUE) << __NUM_TAG_BITS) | __TAG_INT)   145 #define __INTVALUE(VALUE)   ((__attr) {.intvalue=__FROMINT(VALUE)})   146 #define __TOINT(ATTR)       ((ATTR).intvalue >> __NUM_TAG_BITS)   147 #define __MAXINT            ((((__int) 1) << ((sizeof(__int) * 8) - 1 - __NUM_TAG_BITS)) - 1)   148 #define __MININT            (-(((__int) 1) << ((sizeof(__int) * 8) - 1 - __NUM_TAG_BITS)))   149    150 /* Attribute carrying replacement value. */   151    152 #define __REPLACEMENT(ATTR) ((__attr) {.refvalue=(((__ref_tagged) (ATTR).value) | __TAG_REPLACING)})   153 #define __REPLACED(ATTR)    ((__attr) {.value=((__ref) ((ATTR).refvalue & ~__TAG_REPLACING))})   154 #define __REPLACING(ATTR)   ((ATTR).refvalue & __TAG_REPLACING)   155    156 /* Argument lists. */   157    158 #define __ARGS(...)         ((__attr[]) {__VA_ARGS__})   159 #define __KWARGS(...)       ((__param[]) {__VA_ARGS__})   160    161 /* Attribute codes and positions for attribute names. */   162    163 #define __ATTRCODE(ATTRNAME)    __code_##ATTRNAME   164 #define __ATTRPOS(ATTRNAME)     __pos_##ATTRNAME   165 #define __PARAMCODE(PARAMNAME)  __pcode_##PARAMNAME   166 #define __PARAMPOS(PARAMNAME)   __ppos_##PARAMNAME   167    168 #endif /* __TYPES_H__ */