-
Notifications
You must be signed in to change notification settings - Fork 0
/
type_updater.c
229 lines (209 loc) · 7.7 KB
/
type_updater.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
#include <stdlib.h>
#include <stdio.h>
#include "type_updater.h"
#include "type_handler.h"
#include "function.h"
#include "function_set.h"
#include "linked_list.h"
#define DEBUG 0
function_p ut_current_function = NULL;
prototype_p ut_current_p = NULL;
void update_type(tn_pointer node);
void update_type_primary(tn_pointer node){
primary_p value = (primary_p) node->content;
if (node->allowed_types != NULL)
linked_list_destroy_opt_erase(node->allowed_types, false);
linked_list_pointer type_list;
switch(value->t){
case PRIMARY_STRING :{
type_list = new_type_list_single_from_name("i8 *");
break;
}
case PRIMARY_DOUBLE :{
type_list = new_type_list_single_from_name("double");
break;
}
case PRIMARY_INT :{
type_list = new_type_list_single_from_name("i32");
break;
}
}
node->allowed_types = type_list;
}
void update_type_affect(tn_pointer node){
update_type(node->right_child);
linked_list_pointer types;
types = remove_types_not_shared(node->left_child->allowed_types,
node->right_child->allowed_types);
node->allowed_types = types;// list is shared with variable
}
void update_type_childs(tn_pointer node){
update_type(node->left_child);
update_type(node->right_child);
}
void update_type_identifier(tn_pointer node){
variable_p v = get_variable(node->context, node->content);
node->allowed_types = v->allowed_types;
}
void update_type_arithmetic(tn_pointer node){
update_type_childs(node);
if (node->allowed_types != NULL)
linked_list_destroy_opt_erase(node->allowed_types, false);
node->allowed_types = th_arithmetic(node->left_child->allowed_types,
node->right_child->allowed_types);
}
void update_type_logical(tn_pointer node){
update_type_childs(node);
if (node->allowed_types != NULL)
linked_list_destroy_opt_erase(node->allowed_types, false);
node->allowed_types = th_logical(node->left_child->allowed_types,
node->right_child->allowed_types);
}
void update_type_cmp(tn_pointer node){
update_type_childs(node);
if (node->allowed_types != NULL)
linked_list_destroy_opt_erase(node->allowed_types, false);
node->allowed_types = th_comparison(node->left_child->allowed_types,
node->right_child->allowed_types);
}
void update_type_return(tn_pointer node){
update_type(node->left_child);
type_list_add_type_list(node->allowed_types,
node->left_child->allowed_types);
type_list_add_type_list(ut_current_function->possible_return_types,
node->left_child->allowed_types);
}
void update_type_call(tn_pointer node){
function_call_p call = (function_call_p)node->content;
if (node->allowed_types != NULL)
linked_list_destroy_opt_erase(node->allowed_types, false);
node->allowed_types = new_linked_list();
//getting prototypes
linked_list_pointer args = new_linked_list();
if (linked_list_size(call->parameters) > 0){
linked_list_restart(call->parameters);
while(true){
tn_pointer arg = linked_list_get(call->parameters);
update_type(arg);
linked_list_append(args, arg->allowed_types);
if (linked_list_end(call->parameters))
break;
linked_list_next(call->parameters);
}
}
linked_list_pointer matching;
matching = function_set_matching_functions(global_fs,
call->f_called->name,
args);
bool valid_recursion = false;
if (prototype_matches(ut_current_p,args)){
#if DEBUG
printf(";recursion found\n");
#endif
valid_recursion = true;
linked_list_append(matching, ut_current_function);
}
if (linked_list_size(matching) == 0){
linked_list_destroy_opt_erase(matching, false);
linked_list_destroy_opt_erase(args, false);
#if DEBUG
printf(";No functions matching the specified prototype\n");
#endif
return;
}
//printf(";a function matching the specified prototype was found\n");
// adding return_types
linked_list_pointer matching_proto;
matching_proto = function_set_matching_prototypes(global_fs,
call->f_called->name,
args);
if (valid_recursion){
linked_list_append(matching_proto, ut_current_p);
}
if (call->valid_prototypes != NULL)
linked_list_destroy_opt_erase(call->valid_prototypes, false);
call->valid_prototypes = matching_proto;
// For recursion, prototype hasn't received return_type yet
if (valid_recursion){
linked_list_restart(matching);
while(true){
function_p f = (function_p)linked_list_get(matching);
type_list_add_type_list(node->allowed_types,
f->possible_return_types);
if (linked_list_end(matching))
break;
linked_list_next(matching);
}
}
// For usual cases, return_type of prototypes is known
else{
linked_list_restart(matching_proto);
while(true){
prototype_p p = (prototype_p)linked_list_get(matching_proto);
type_list_add(node->allowed_types,
p->return_type);
if (linked_list_end(matching_proto))
break;
linked_list_next(matching_proto);
}
}
linked_list_destroy_opt_erase(matching, false);
linked_list_destroy_opt_erase(args, false);
}
void update_type_conditional(tn_pointer node){
conditional_block_p cond = node->content;
update_type(cond->condition);
update_type(cond->true_case);
update_type(cond->false_case);
}
void update_type_while(tn_pointer node){
update_type_childs(node);
}
void update_type_for(tn_pointer node){
for_block_p content = (for_block_p)node->content;
update_type(content->from_expr);
update_type(content->to_expr);
update_type(content->code);
}
void update_type(tn_pointer node){
if (node == NULL)
return;
switch(node->type){
case PRIMARY : update_type_primary(node); break;
case AFFECT : update_type_affect(node); break;
case LIST : update_type_childs(node); break;
case IDENTIFIER : update_type_identifier(node); break;
case ADDITION : update_type_arithmetic(node); break;
case SUBSTRACTION : update_type_arithmetic(node); break;
case MULTIPLY : update_type_arithmetic(node); break;
case DIVIDE : update_type_arithmetic(node); break;
case OR_NODE : update_type_logical(node); break;
case AND_NODE : update_type_logical(node); break;
// function should be updated on it's own
case FUNCTION : break;
case RETURN_NODE : update_type_return(node); break;
case CALL : update_type_call(node); break;
case NEQ_NODE : update_type_cmp(node); break;
case EQ_NODE : update_type_cmp(node); break;
case LESS_NODE : update_type_cmp(node); break;
case LEQ_NODE : update_type_cmp(node); break;
case GEQ_NODE : update_type_cmp(node); break;
case GREATER_NODE : update_type_cmp(node); break;
case IF_NODE : update_type_conditional(node); break;
case WHILE_NODE : update_type_while(node); break;
case FOR_NODE : update_type_for(node); break;
default: break;
}
return;
}
void update_function(function_p f, prototype_p p){
ut_current_function = f;
ut_current_p = p;
linked_list_destroy_opt_erase(f->possible_return_types, false);
// Might be needed but implies some problems for now
if (f->root->allowed_types != NULL)
linked_list_destroy_opt_erase(f->root->allowed_types, false);
f->possible_return_types = new_linked_list();
f->root->allowed_types = new_linked_list();
update_type(f->root);
}