code / tomo

Lines41.3K C23.7K Markdown9.7K YAML5.0K Tomo2.3K
7 others 763
Python231 Shell230 make212 INI47 Text21 SVG16 Lua6
(103 lines)
1 // Type information and methods for TypeInfos (i.e. runtime representations of types)
3 #pragma once
5 #include <stdbool.h>
6 #include <stdint.h>
7 #include <stdio.h>
9 #include "datatypes.h"
11 typedef struct TypeInfo_s TypeInfo_t;
13 typedef void (*serialize_fn_t)(const void *, FILE *, Table_t *, const TypeInfo_t *);
14 typedef void (*deserialize_fn_t)(FILE *, void *, List_t *, const TypeInfo_t *);
15 typedef bool (*is_none_fn_t)(const void *, const TypeInfo_t *);
16 typedef uint64_t (*hash_fn_t)(const void *, const TypeInfo_t *);
17 typedef int32_t (*compare_fn_t)(const void *, const void *, const TypeInfo_t *);
18 typedef bool (*equal_fn_t)(const void *, const void *, const TypeInfo_t *);
19 typedef Text_t (*as_text_fn_t)(const void *, bool, const TypeInfo_t *);
21 typedef struct {
22 hash_fn_t hash;
23 compare_fn_t compare;
24 equal_fn_t equal;
25 as_text_fn_t as_text;
26 is_none_fn_t is_none;
27 serialize_fn_t serialize;
28 deserialize_fn_t deserialize;
29 } metamethods_t;
31 typedef struct {
32 const char *name;
33 const TypeInfo_t *type;
34 } NamedType_t;
36 struct TypeInfo_s {
37 int64_t size, align;
38 metamethods_t metamethods;
39 struct { // Anonymous tagged union for convenience
40 enum {
41 OpaqueInfo,
42 StructInfo,
43 EnumInfo,
44 PointerInfo,
45 TextInfo,
46 ListInfo,
47 TableInfo,
48 FunctionInfo,
49 OptionalInfo,
50 TypeInfoInfo
51 } tag;
52 union {
53 struct {
54 } OpaqueInfo;
55 struct {
56 const char *sigil;
57 const TypeInfo_t *pointed;
58 } PointerInfo;
59 struct {
60 const char *lang;
61 } TextInfo;
62 struct {
63 const TypeInfo_t *item;
64 } ListInfo;
65 struct {
66 const TypeInfo_t *key, *value;
67 } TableInfo;
68 struct {
69 const char *type_str;
70 } FunctionInfo;
71 struct {
72 const char *type_str;
73 } TypeInfoInfo;
74 struct {
75 const TypeInfo_t *type;
76 } OptionalInfo;
77 struct {
78 const char *name;
79 NamedType_t *tags;
80 int num_tags;
81 } EnumInfo;
82 struct {
83 const char *name;
84 NamedType_t *fields;
85 int num_fields;
86 bool is_secret : 1, is_opaque : 1;
87 } StructInfo;
88 };
89 };
90 };
92 extern const TypeInfo_t Void$info;
93 extern const TypeInfo_t Abort$info;
95 Text_t Type$as_text(const void *typeinfo, bool colorize, const TypeInfo_t *type);
97 #define Type$info(typestr) \
98 &((TypeInfo_t){ \
99 .size = sizeof(TypeInfo_t), \
100 .align = __alignof__(TypeInfo_t), \
101 .tag = TypeInfoInfo, \
102 .TypeInfoInfo.type_str = typestr, \
103 .metamethods = {.serialize = cannot_serialize, .deserialize = cannot_deserialize, .as_text = Type$as_text}})