Lichen

templates/types.h

944:50706c4011d4
2021-11-02 Paul Boddie Merged changes from the default branch. trailing-data
     1 /* Runtime types.     2      3 Copyright (C) 2015, 2016, 2017, 2018, 2019,     4               2021 Paul Boddie <paul@boddie.org.uk>     5      6 This program is free software; you can redistribute it and/or modify it under     7 the terms of the GNU General Public License as published by the Free Software     8 Foundation; either version 3 of the License, or (at your option) any later     9 version.    10     11 This program is distributed in the hope that it will be useful, but WITHOUT    12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS    13 FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more    14 details.    15     16 You should have received a copy of the GNU General Public License along with    17 this program.  If not, see <http://www.gnu.org/licenses/>.    18 */    19     20 #ifndef __TYPES_H__    21 #define __TYPES_H__    22     23 /* Define code and position types, populated by enum values defined for each    24    program specifically. */    25     26 #include <stdint.h>    27 #include <stdlib.h>    28     29 /* Include the special instance position value. The pos member of __obj refers    30    to the special type attribute for classes, indicating which position holds    31    the attribute describing the class type. For instances, it is set to the same    32    attribute position as __class__ and is defined in the following file. */    33     34 #include "instancepos.h"    35     36 typedef uint16_t __code;    37 typedef uint16_t __pos;    38 typedef uint16_t __pcode;    39 typedef uint16_t __ppos;    40     41 /* Attribute tables are lists of codes confirming the presence of attributes. */    42     43 typedef struct __table    44 {    45     const __pos size;    46     const __code attrs[];    47 } __table;    48     49 /* Parameter tables are lists of codes confirming the presence of parameters, as    50    well as the positions of those parameters in the list for a given function.    51 */    52     53 typedef struct __param    54 {    55     __code code;    56     __pos pos;    57 } __param;    58     59 typedef struct __ptable    60 {    61     const __ppos min, max, size;    62     const __param params[];    63 } __ptable;    64     65 /* Attributes are values referring to objects or encoding other information.    66    Objects are collections of attributes.    67    Object references are references to tables and collections of attributes.    68    Attribute references are references to single attributes. */    69     70 typedef struct __obj __obj;    71 typedef struct __fragment __fragment;    72 typedef union __attr __attr;    73 typedef __obj * __ref;    74     75 /* Introduce an integer type that should not exceed the size of the pointer    76    type. */    77     78 typedef ssize_t __int;    79     80 /* Attribute value interpretations. */    81     82 typedef union __attr    83 {    84     /* General attribute members. */    85     86     __ref value;                /* attribute value */    87     __int intvalue;             /* integer value data (shifted value, tagged) */    88     89     /* Special case attribute members. */    90     91     const __ptable * ptable;    /* parameter table */    92     struct {    93         __pcode code;           /* parameter table code for key */    94         __ppos pos;             /* parameter table position for key */    95     };    96     __attr (*fn)();             /* callable details */    97     char * strvalue;            /* string value */    98     __fragment * seqvalue;      /* sequence data */    99     void * datavalue;           /* object-specific data */   100 } __attr;   101    102 typedef struct __obj   103 {   104     const __table * table;      /* attribute table */   105     __ppos pos;                 /* position of attribute indicating class */   106     __attr attrs[];             /* attributes */   107    108     /* Specialisations of this type may add other members.   109        See generator.py for type generation, progops.h for definitions, and   110        the generated progtypes.h for the final details. */   111    112 } __obj;   113    114 #define __INSTANCE_SIZE(NUMBER) ((NUMBER) * sizeof(__attr) + sizeof(__table *) + sizeof(__ppos))   115    116 /* Fragments are simple collections of attributes employed by sequence types.   117    They provide the basis of lists and tuples. */   118    119 typedef struct __fragment   120 {   121     __int size, capacity;   122     __attr attrs[];   123 } __fragment;   124    125 #define __FRAGMENT_SIZE(NUMBER) ((NUMBER) * sizeof(__attr) + 2 * sizeof(__int))   126    127 /* Attribute interpretation. */   128    129 #define __NUM_TAG_BITS      2   130 #define __TAG_INT           0b01   131 #define __TAG_MASK          0b11   132 #define __INTEGER(ATTR)     (((ATTR).intvalue & __TAG_MASK) == __TAG_INT)   133    134 /* Attribute value setting. */   135    136 #define __ATTRVALUE(VALUE)  ((__attr) {.value=(__ref) VALUE})   137 #define __NULL              __ATTRVALUE(0)   138 #define __SETNULL(ATTR)     ((ATTR).value = 0)   139    140 /* Attribute as instance setting. */   141    142 #define __INTVALUE(VALUE)   ((__attr) {.intvalue=(((__int) VALUE) << __NUM_TAG_BITS) | __TAG_INT})   143 #define __TOINT(ATTR)       ((ATTR).intvalue >> __NUM_TAG_BITS)   144 #define __MAXINT            ((((__int) 1) << ((sizeof(__int) * 8) - 1 - __NUM_TAG_BITS)) - 1)   145 #define __MININT            (-(((__int) 1) << ((sizeof(__int) * 8) - 1 - __NUM_TAG_BITS)))   146    147 /* Argument lists. */   148    149 #define __ARGS(...)         ((__attr[]) {__VA_ARGS__})   150 #define __KWARGS(...)       ((__param[]) {__VA_ARGS__})   151    152 /* Attribute codes and positions for attribute names. */   153    154 #define __ATTRCODE(ATTRNAME)    __code_##ATTRNAME   155 #define __ATTRPOS(ATTRNAME)     __pos_##ATTRNAME   156 #define __PARAMCODE(PARAMNAME)  __pcode_##PARAMNAME   157 #define __PARAMPOS(PARAMNAME)   __ppos_##PARAMNAME   158    159 #endif /* __TYPES_H__ */