~pixelherodev/c3lib

ref: ddd789667e0b3f1f92a636752601943924d590c9 c3lib/c3.c -rw-r--r-- 5.7 KiB
ddd78966Noam Preil tmp 3 months ago
                                                                                
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#include <stddef.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>

#include "tags.h"
#include "c3.h"
#include "util.h"
#include "stb.h"

void
c3init(c3ctx *ctx)
{
	// TODO: stop using this workaround
	c3ctx result = {
		.core = {
			.version = 0,
			.strings = NULL,
			.chars = NULL,
		},
		.ast = {
			.nodes = NULL,
		},
		.analysis = {
			.tests = NULL,
			.exports = NULL,
			.types = NULL,
			.deps = NULL,
			.callgraph = NULL,
			.names = NULL,
		},
		.debug = {
			.linenumbers = NULL,
			.columnnumbers = NULL,
			.linestrings = NULL,
		},
		.dedup = {
			.types = NULL,
		},
	};
	if(c3append(&result, C3_CONTEXTUAL, 1, 0) == -1)
		FATAL("OOM", 0);
	*ctx = result;
}

void
c3free(c3ctx *ctx)
{
	stb_sb_free(ctx->ast.nodes);
	stb_sb_free(ctx->core.strings);
	stb_sb_free(ctx->core.chars);
	free(ctx);
}

#define WFAIL() {ERROR("Failed to write ctx", 0); return 0;}
#define RFAIL() {ERROR("Failed to read ctx",0);return 0;}

static int
c3ctx_read_array(void *_array, uint32_t elemsize, int fd)
{
	uint32_t count;
	void **array = _array;
	if(read(fd, &count, 4) != 4)
		return 0;
	if(!stb_sb_ensure_capacity(array, count, elemsize))
		return 0;
	if(read(fd, *array, count * elemsize) != count * elemsize)
		return 0;
	stb_sb_increment(*array, count);
	return 1;
}

static int
c3ctx_write_array(void *_array, uint32_t elemsize, int fd)
{
	void **array = _array;
	uint32_t count = stb_sb_count(*array);
	if(write(fd, &count, 4) != 4)
		return 0;
	if(write(fd, *array, count * elemsize) != count * elemsize)
		return 0;
	return 1;
}

static int
c3ctxwritemap(c3nodemap *map, int fd)
{
	return 1;
}

static int
c3corewrite(c3core core, int fd)
{
	if(write(fd, &core.version, 4) != 4)
		WFAIL();
	if(!c3ctx_write_array(&core.strings, sizeof(c3string), fd))
		WFAIL();
	if(!c3ctx_write_array(&core.chars, 1, fd))
		WFAIL();
	return 1;
}

static int
c3astwrite(c3ast ast, int fd)
{
	if(!c3ctx_write_array(&ast.nodes, 4, fd))
		WFAIL();
	return 1;
}

static int
c3analysiswrite(c3analysis analysis, int fd)
{
	if(!c3ctxwritemap(analysis.deps, fd))
		WFAIL();
	return 1;
}

int
c3write(c3ctx *ctx, int fd)
{
	if(!c3corewrite(ctx->core, fd))
		WFAIL();
	if(!c3astwrite(ctx->ast, fd))
		WFAIL();
	if(!c3analysiswrite(ctx->analysis, fd))
		WFAIL();
	return 1;
}

static int
c3coreread(c3core *core, int fd)
{
	if(read(fd, &core->version, 4) != 4)
		RFAIL();
	if(!c3ctx_read_array(&core->strings, sizeof(c3string), fd))
		RFAIL();
	if(!c3ctx_read_array(&core->chars, 1, fd))
		RFAIL();
	return 1;
}

int
c3read(c3ctx *ctx, int fd)
{
	c3init(ctx);
	if(!c3coreread(&ctx->core, fd))
		RFAIL();
	if(!c3ctx_read_array(&ctx->ast.nodes, 4, fd))
		RFAIL();
	return 1;
}

uint32_t
c3append(c3ctx *ctx, c3tag tag, uint16_t kid_count, ...)
{
	va_list args;
	uint32_t i;
	uint32_t index;
	if(!stb_sb_ensure_capacity(&ctx->ast.nodes, 1 + kid_count, 4))
		return -1;
	index = stb_sb_count(ctx->ast.nodes);
	c3nodeset(*ctx, index, tag, kid_count);
	// A single node actually takes up 8 bytes, or two uint32 entries
	va_start(args, kid_count);
	for(i = 0; i < kid_count; i += 1)
		ctx->ast.nodes[index + 1 + i].node_index = va_arg(args, uint32_t);
	stb_sb_increment(ctx->ast.nodes, kid_count + 1);
	va_end(args);
	return index;
}

uint32_t
c3appendarr(c3ctx *ctx, c3tag tag, uint16_t kid_count, uint32_t *kids)
{
	uint32_t i;
	uint32_t index;
	if(!stb_sb_ensure_capacity(&ctx->ast.nodes, 1 + kid_count, 4))
		return -1;
	index = stb_sb_count(ctx->ast.nodes);
	c3nodeset(*ctx, index, tag, kid_count);
	// A single node actually takes up 8 bytes, or two uint32 entries
	for(i = 0; i < kid_count; i += 1)
		ctx->ast.nodes[index + 1 + i].node_index = kids[i];
	stb_sb_increment(ctx->ast.nodes, kid_count + 1);
	return index;
}

uint32_t
c3append_chars(c3ctx *ctx, char *ptr, uint32_t len)
{
	uint32_t index;
	if(!stb_sb_ensure_capacity(&ctx->core.chars, len, 1))
		return -1;
	index = stb_sb_count(ctx->core.chars);
	strncpy(ctx->core.chars + index, ptr, len);
	stb_sb_increment(ctx->core.chars, len);
	return index;
}

uint32_t
c3append_string(c3ctx *ctx, char *ptr, uint32_t len)
{
	uint32_t index, cindex;
	if(!stb_sb_ensure_capacity(&ctx->core.strings, 1, sizeof(c3string)))
		return -1;
	cindex = c3append_chars(ctx, ptr, len);
	if(cindex == -1)
		return -1;
	index = stb_sb_count(ctx->core.strings);
	ctx->core.strings[index] = (c3string){cindex, len};
	stb_sb_increment(ctx->core.strings, 1);
	return index;
}

char *
h_c3str(c3ctx ctx, uint32_t string_handle)
{
	char *buf;
	c3string string;
	string = ctx.core.strings[string_handle];
	buf = malloc(string.length + 1);
	if(buf == NULL)
		return NULL;
	strncpy(buf, &ctx.core.chars[string.index], string.length);
	buf[string.length] = 0;
	return buf;
}

uint32_t
c3nodechild(c3ctx ctx, uint32_t node, uint32_t child)
{
	uint32_t index;
	index = node + 1 + child;
	if(index >= stb_sb_count(ctx.ast.nodes) || ctx.ast.nodes[index].node_index >= stb_sb_count(ctx.ast.nodes))
		return -1;
	return ctx.ast.nodes[index].node_index;
}

c3tag
c3nodetag(c3ctx ctx, uint32_t node)
{
	return ctx.ast.nodes[node].tag;
}

uint16_t
c3nodekids(c3ctx ctx, uint32_t node)
{
	return ctx.ast.nodes[node].kids;
}

void
c3nodeset(c3ctx ctx, uint32_t index, c3tag tag, uint16_t kid_count)
{
	void *raw;
	c3node *ptr;
	c3node node;
	node.tag = tag;
	node.kids = kid_count;
	if(index > stb_sb_count(ctx.ast.nodes))
		FATAL("ICE: index out of bounds: %d", index);
	raw = &ctx.ast.nodes[index];
	ptr = raw;
	*ptr = node;
}

uint32_t
c3intern(c3ctx *ctx, char *ptr, uint32_t len)
{
	uint32_t handle;
	uint32_t count;
	count = stb_sb_count(ctx->core.strings);
	for(handle = 0; handle < count; handle += 1)
		if(len == ctx->core.strings[handle].length)
			if(strncmp(ctx->core.chars + ctx->core.strings[handle].index, ptr, len) == 0)
				return handle;
	return c3append_string(ctx, ptr, len);
}