-
-
Notifications
You must be signed in to change notification settings - Fork 415
/
frame.c
297 lines (254 loc) · 6.26 KB
/
frame.c
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#include "frame.h"
#include "ast.h"
#include "error.h"
#include "../../libponyrt/mem/pool.h"
#include "ponyassert.h"
#include <string.h>
static bool push_frame(typecheck_t* t)
{
typecheck_frame_t* f = POOL_ALLOC(typecheck_frame_t);
if(t->frame != NULL)
{
memcpy(f, t->frame, sizeof(typecheck_frame_t));
f->prev = t->frame;
} else {
memset(f, 0, sizeof(typecheck_frame_t));
}
t->frame = f;
return true;
}
bool frame_push(typecheck_t* t, ast_t* ast)
{
bool pop = false;
if(ast == NULL)
{
typecheck_frame_t* f = POOL_ALLOC(typecheck_frame_t);
memset(f, 0, sizeof(typecheck_frame_t));
f->prev = t->frame;
t->frame = f;
return true;
}
switch(ast_id(ast))
{
case TK_PROGRAM:
pop = push_frame(t);
t->frame->package = NULL;
t->frame->module = NULL;
break;
case TK_PACKAGE:
// Can occur in a module as a result of a use expression.
pop = push_frame(t);
t->frame->package = ast;
t->frame->module = NULL;
break;
case TK_MODULE:
pop = push_frame(t);
t->frame->module = ast;
t->frame->ifdef_cond = NULL;
t->frame->ifdef_clause = ast;
break;
case TK_INTERFACE:
case TK_TRAIT:
case TK_PRIMITIVE:
case TK_STRUCT:
case TK_CLASS:
case TK_ACTOR:
pop = push_frame(t);
t->frame->type = ast;
break;
case TK_PROVIDES:
pop = push_frame(t);
t->frame->provides = ast;
break;
case TK_NEW:
case TK_BE:
case TK_FUN:
pop = push_frame(t);
t->frame->method = ast;
break;
case TK_TYPEARGS:
pop = push_frame(t);
t->frame->method_type = NULL;
t->frame->ffi_type = NULL;
t->frame->local_type = NULL;
t->frame->constraint = NULL;
t->frame->iftype_constraint = NULL;
break;
case TK_CASE:
pop = push_frame(t);
t->frame->the_case = ast;
break;
case TK_WHILE:
case TK_REPEAT:
pop = push_frame(t);
t->frame->loop = ast;
break;
case TK_TRY:
case TK_TRY_NO_CHECK:
pop = push_frame(t);
t->frame->try_expr = ast;
break;
case TK_RECOVER:
pop = push_frame(t);
t->frame->recover = ast;
break;
default:
{
ast_t* parent = ast_parent(ast);
if(parent == NULL)
return pop;
switch(ast_id(parent))
{
case TK_TYPEPARAM:
{
AST_GET_CHILDREN(parent, id, constraint, def_type);
if(constraint == ast)
{
pop = push_frame(t);
t->frame->constraint = ast;
}
break;
}
case TK_NEW:
case TK_BE:
case TK_FUN:
{
AST_GET_CHILDREN(parent,
cap, id, typeparams, params, result, error, body);
if(params == ast)
{
pop = push_frame(t);
t->frame->method_params = ast;
} else if(result == ast) {
pop = push_frame(t);
t->frame->method_type = ast;
} else if(body == ast) {
pop = push_frame(t);
t->frame->method_body = ast;
}
break;
}
case TK_TYPEARGS:
{
switch(ast_id(ast_parent(parent)))
{
case TK_FFIDECL:
case TK_FFICALL:
pop = push_frame(t);
t->frame->ffi_type = ast;
break;
default: {}
}
break;
}
case TK_PARAM:
{
AST_GET_CHILDREN(parent, id, type, def_arg);
if(type == ast)
{
pop = push_frame(t);
t->frame->local_type = ast;
} else if(def_arg == ast) {
pop = push_frame(t);
t->frame->def_arg = ast;
}
break;
}
case TK_VAR:
case TK_LET:
if(ast_childidx(parent, 1) == ast)
{
pop = push_frame(t);
t->frame->local_type = ast;
}
break;
case TK_CASE:
if(ast_child(parent) == ast)
{
pop = push_frame(t);
t->frame->pattern = ast;
}
break;
case TK_AS:
{
AST_GET_CHILDREN(parent, expr, type);
if(type == ast)
{
pop = push_frame(t);
t->frame->as_type = ast;
}
break;
}
case TK_WHILE:
{
AST_GET_CHILDREN(parent, cond, body, else_clause);
if(cond == ast)
{
pop = push_frame(t);
t->frame->loop_cond = ast;
} else if(body == ast) {
pop = push_frame(t);
t->frame->loop_body = ast;
} else if(else_clause == ast) {
pop = push_frame(t);
t->frame->loop_else = ast;
}
break;
}
case TK_REPEAT:
{
AST_GET_CHILDREN(parent, body, cond, else_clause);
if(body == ast)
{
pop = push_frame(t);
t->frame->loop_body = ast;
} else if(cond == ast) {
pop = push_frame(t);
t->frame->loop_cond = ast;
} else if(else_clause == ast) {
pop = push_frame(t);
t->frame->loop_else = ast;
}
break;
}
case TK_IFDEF:
{
AST_GET_CHILDREN(parent, cond, body, else_clause, else_cond);
if(body == ast)
{
pop = push_frame(t);
t->frame->ifdef_cond = cond;
t->frame->ifdef_clause = body;
} else if(else_clause == ast) {
pop = push_frame(t);
t->frame->ifdef_cond = else_cond;
t->frame->ifdef_clause = else_clause;
}
break;
}
case TK_IFTYPE:
{
AST_GET_CHILDREN(parent, l_type, r_type, body);
pop = push_frame(t);
if(r_type == ast)
{
t->frame->iftype_constraint = ast;
} else if(body == ast) {
t->frame->iftype_body = ast;
}
break;
}
default: {}
}
break;
}
}
return pop;
}
void frame_pop(typecheck_t* t)
{
typecheck_frame_t* f = t->frame;
pony_assert(f != NULL);
t->frame = f->prev;
POOL_FREE(typecheck_frame_t, f);
}