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