~pixelherodev/zyg

ref: c843bae17984022c2d9a134fa842ef53c114cd7f zyg/sema.c -rw-r--r-- 2.1 KiB
c843bae1Noam Preil [driver] check fread 2 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
#include <stdint.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>

#include "c3/tags.h"
#include "c3/c3.h"
#include "c3/util.h"
#include "c3/sema/scope.h"
#include "c3/stb_ds.h"

#include "package.h"
#include "context.h"

int resolve_tree(c3ctx *);
int serialize_deps(c3ctx *);
// needs to be able to patch the source...
int t_init(c3ctx *);
int t_node(uint32_t source);
int expr_init(c3ctx *);
uint32_t l_fn(uint32_t index);
int expr_finish(void);
int expr_gen_test(void);

static void
usagedump(c3ctx c3)
{
	uint32_t i;
	uint32_t dep_node;
	uint32_t kids = stbds_hmlenu(c3.analysis.deps);
	for(i = 0; i < kids; i += 1){
		dep_node = c3.analysis.deps[i].value;
		printf("%d: %u: ", i, dep_node);
		c3nodedump(c3, dep_node, 0, 1);
		printf("\n");
	}
}

static int
anal_execute(c3ctx *c3)
{
	uint32_t i;
	uint32_t t;
	uint16_t kids = stbds_hmlenu(c3->analysis.deps);
	for(i = 0; i < kids; i += 1){
		t = c3->analysis.deps[i].key;
		// TODO: comptime engine
		if(!t_node(t)){
			ERROR("Type analysis failed!", 0);
			return 0;
		}
		// If function and has body (i.e. not external), generate IR
		if(c3nodetag(*c3, t) == C3_VALUE_FN && c3nodechild(*c3, t, 1) != -1){
			if(l_fn(t) == UINT32_MAX){
				ERROR("Failed to extract expression semantics", 0);
				return 0;
			}
		}
	}
	return 1;
}

static int
anal_finish(c3ctx *c3, int good, int out)
{
	char c = 1;
	if(good && !expr_finish())
		return 0;
	if(!good)
		return 0;
	if(out == -1)
		return 1;
	if(write(out, &c, 1) != 1){
		ERROR("Failed to write c3 message type", 0);
		return 0;
	}
	if(!c3write(c3, out)){
		ERROR("Failed to send c3 to parent", 0);
		return 0;
	}
	return 1;
}

int
analysis(c3ctx *c3, int out)
{
	int good;
	if(!t_init(c3))
		return 0;
	if(!expr_init(c3))
		return 0;
	if(!resolve_tree(c3)){
		ERROR("Tree resolution failed!", 0);
		return 0;
	}
	scope_free();
	if(c3->analysis.deps == NULL){
		ERROR("ICE: dependency graph not found, old analysis is gone!", 0);
		return 0;
	}
	if(zyg_ctx.tests)
		if(!expr_gen_test())
			return 0;
	if(!serialize_deps(c3))
		return 0;
	if(zyg_ctx.usage_debug)
		usagedump(*c3);
	good = anal_execute(c3);
	return anal_finish(c3, good, out);
}