code / tomo

Lines41.3K C23.7K Markdown9.7K YAML5.0K Tomo2.3K
7 others 763
Python231 Shell230 make212 INI47 Text21 SVG16 Lua6
(170 lines)
1 // Logic for defining and working with types
3 #pragma once
5 #include <stdlib.h>
7 #include "ast.h"
9 typedef struct type_s type_t;
11 typedef struct arg_s {
12 const char *name, *alias;
13 type_t *type;
14 ast_t *default_val;
15 Text_t comment;
16 struct arg_s *next;
17 } arg_t;
19 #define ARG_LIST(...) \
20 ({ \
21 arg_t *args[] = {__VA_ARGS__}; \
22 for (size_t i = 0; i < sizeof(args) / sizeof(args[0]) - 1; i++) \
23 args[i]->next = args[i + 1]; \
24 args[0]; \
25 })
27 typedef struct tag_s {
28 const char *name;
29 int64_t tag_value;
30 type_t *type;
31 struct tag_s *next;
32 } tag_t;
34 typedef struct type_list_s {
35 type_t *type;
36 struct type_list_s *next;
37 } type_list_t;
39 struct type_s {
40 enum {
41 UnknownType,
42 AbortType,
43 ReturnType,
44 VoidType,
45 MemoryType,
46 BoolType,
47 ByteType,
48 BigIntType,
49 IntType,
50 NumType,
51 CStringType,
52 TextType,
53 PathType,
54 ListType,
55 TableType,
56 FunctionType,
57 ClosureType,
58 PointerType,
59 StructType,
60 EnumType,
61 OptionalType,
62 TypeInfoType,
63 ModuleType,
64 } tag;
66 union {
67 struct {
68 } UnknownType, AbortType, VoidType, MemoryType, BoolType, PathType;
69 struct {
70 type_t *ret;
71 } ReturnType;
72 struct {
73 } BigIntType;
74 struct {
75 enum { TYPE_IBITS8 = 8, TYPE_IBITS16 = 16, TYPE_IBITS32 = 32, TYPE_IBITS64 = 64 } bits;
76 } IntType;
77 struct {
78 } ByteType;
79 struct {
80 enum { TYPE_NBITS32 = 32, TYPE_NBITS64 = 64 } bits;
81 } NumType;
82 struct {
83 } CStringType;
84 struct {
85 const char *lang;
86 struct env_s *env;
87 } TextType;
88 struct {
89 type_t *item_type;
90 } ListType;
91 struct {
92 type_t *key_type, *value_type;
93 struct env_s *env;
94 ast_t *default_value;
95 } TableType;
96 struct {
97 arg_t *args;
98 type_t *ret;
99 } FunctionType;
100 struct {
101 type_t *fn;
102 } ClosureType;
103 struct {
104 type_t *pointed;
105 bool is_stack : 1;
106 } PointerType;
107 struct {
108 const char *name;
109 arg_t *fields;
110 struct env_s *env;
111 bool opaque : 1, external : 1;
112 } StructType;
113 struct {
114 const char *name;
115 tag_t *tags;
116 struct env_s *env;
117 bool opaque;
118 } EnumType;
119 struct {
120 type_t *type;
121 } OptionalType;
122 struct {
123 const char *name;
124 type_t *type;
125 struct env_s *env;
126 } TypeInfoType;
127 struct {
128 const char *name;
129 } ModuleType;
130 } __data;
133 #define Type(typetag, ...) new (type_t, .tag = typetag, .__data.typetag = {__VA_ARGS__})
134 #define INT_TYPE Type(BigIntType)
135 #define NUM_TYPE Type(NumType, .bits = TYPE_NBITS64)
136 #define NewFunctionType(ret, ...) \
137 _make_function_type(ret, sizeof((arg_t[]){__VA_ARGS__}) / sizeof(arg_t), (arg_t[]){__VA_ARGS__})
139 Text_t type_to_text(type_t *t);
140 Text_t arg_types_to_text(arg_t *args, const char *separator);
141 const char *get_type_name(type_t *t);
142 PUREFUNC bool type_eq(type_t *a, type_t *b);
143 PUREFUNC bool type_is_a(type_t *t, type_t *req);
144 type_t *type_or_type(type_t *a, type_t *b);
145 type_t *value_type(type_t *a);
146 PUREFUNC bool is_discardable_type(type_t *t);
147 typedef enum {
148 NUM_PRECISION_EQUAL,
149 NUM_PRECISION_LESS,
150 NUM_PRECISION_MORE,
151 NUM_PRECISION_INCOMPARABLE
152 } precision_cmp_e;
153 PUREFUNC precision_cmp_e compare_precision(type_t *a, type_t *b);
154 bool has_heap_memory(type_t *t);
155 bool has_refcounts(type_t *t);
156 bool has_stack_memory(type_t *t);
157 PUREFUNC bool can_promote(type_t *actual, type_t *needed);
158 PUREFUNC const char *enum_single_value_tag(type_t *enum_type, type_t *t);
159 PUREFUNC bool is_int_type(type_t *t);
160 PUREFUNC bool is_numeric_type(type_t *t);
161 PUREFUNC bool is_packed_data(type_t *t);
162 CONSTFUNC bool is_incomplete_type(type_t *t);
163 CONSTFUNC type_t *most_complete_type(type_t *t1, type_t *t2);
164 PUREFUNC size_t type_size(type_t *t);
165 PUREFUNC size_t type_align(type_t *t);
166 PUREFUNC size_t unpadded_struct_size(type_t *t);
167 PUREFUNC type_t *non_optional(type_t *t);
168 type_t *get_field_type(type_t *t, const char *field_name);
169 PUREFUNC type_t *get_iterated_type(type_t *t);
170 type_t *_make_function_type(type_t *ret, int n, arg_t args[n]);