summaryrefslogtreecommitdiff
path: root/proc.h
blob: 3ef49a9da4c000390affd40b4f64e6bb6e8172cb (plain)
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
#ifndef PROC_H
#define PROC_H

#include "ir.h"
#include "xar.h"

typedef struct NameBinding {
	struct NameBinding *prev;
	LexSpan src_pos;
	Str name;
	Node *node;
	unsigned nestlvl;
} NameBinding;

typedef struct ScopeFrame {
	struct ScopeFrame *prev;
	NameBinding *latest;
} ScopeFrame;

typedef struct {
	NameBinding *bind;
	Node *from, *to;
	LexSpan from_pos, to_pos;
} ScopeChange;

typedef struct ScopeChangeList {
	XAR(ScopeChange);
	struct ScopeChangeList *prev;
	Arena *arena;
} ScopeChangeList;

typedef struct {
	Arena *arena;
	ScopeFrame *tail, *free_scope;
	NameBinding *free_bind;
	ScopeChangeList *changes;
	unsigned nestlvl;
} Scope;

ScopeFrame *scope_push(Scope *scope);
ScopeFrame *scope_pop(Scope *scope, Graph *g);
NameBinding *scope_find(Scope *scope, Str name);
NameBinding *scope_bind(Scope *scope, Str name, Node *value, LexSpan pos, Graph *g);
NameBinding *scope_update(Scope *scope, NameBinding *b, Node *to, Graph *g);

void scope_changelist_update(ScopeChangeList *l, NameBinding *b, Node *from, Node *to, Graph *g);
void scope_changelist_push(Scope *scope, ScopeChangeList *l, Arena *a);
void scope_changelist_pop(Scope *scope, Graph *g);
void scope_changelist_merge(Scope *scope, ScopeChangeList *y, ScopeChangeList *n, Node *region, Graph *graph, Arena *scratch);
void scope_changelist_discard(ScopeChangeList *l, Graph *g);

typedef struct {
	Str name;
	Type ret_type;
	Arena perm, scratch;
	NodePool pool;
	Graph graph;
	Scope scope;
} Proc;

void proc_init(Proc *proc, Str name);
void proc_free(Proc *proc);

#endif