-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmatch_type.c
233 lines (206 loc) · 7.22 KB
/
match_type.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
#include <config.h>
#include "match_type.h"
#include <assert.h>
#include "type_t.h"
#include "ast_t.h"
#include "semantic_t.h"
#include "type_hash.h"
#include "adt/error.h"
static inline void match_error(type_t *variant, type_t *concrete,
const source_position_t source_position)
{
print_error_prefix(source_position);
fprintf(stderr, "can't match variant type ");
print_type(variant);
fprintf(stderr, " against ");
print_type(concrete);
fprintf(stderr, "\n");
}
static bool matched_type_variable(type_variable_t *type_variable, type_t *type,
const source_position_t source_position,
bool report_errors)
{
type_t *current_type = type_variable->current_type;
if (current_type != NULL && current_type != type) {
if (report_errors) {
print_error_prefix(source_position);
fprintf(stderr, "ambiguous matches found for type variable '%s': ",
type_variable->base.symbol->string);
print_type(current_type);
fprintf(stderr, ", ");
print_type(type);
fprintf(stderr, "\n");
}
/* are both types normalized? */
assert(typehash_contains(current_type));
assert(typehash_contains(type));
return false;
}
type_variable->current_type = type;
return true;
}
static bool match_compound_type(compound_type_t *variant_type,
type_t *concrete_type,
const source_position_t source_position,
bool report_errors)
{
type_variable_t *type_parameters = variant_type->type_parameters;
if (type_parameters == NULL) {
if (concrete_type != (type_t*) variant_type) {
if (report_errors)
match_error((type_t*) variant_type, concrete_type,
source_position);
return false;
}
return true;
}
if (concrete_type->kind != TYPE_BIND_TYPEVARIABLES) {
if (report_errors)
match_error((type_t*) variant_type, concrete_type, source_position);
return false;
}
bind_typevariables_type_t *bind_typevariables
= (bind_typevariables_type_t*) concrete_type;
compound_type_t *polymorphic_type
= bind_typevariables->polymorphic_type;
if (polymorphic_type != variant_type) {
if (report_errors)
match_error((type_t*) variant_type, concrete_type, source_position);
return false;
}
type_variable_t *type_parameter = type_parameters;
type_argument_t *type_argument = bind_typevariables->type_arguments;
bool result = true;
while (type_parameter != NULL) {
assert(type_argument != NULL);
if (!matched_type_variable(type_parameter, type_argument->type,
source_position, true))
result = false;
type_parameter = type_parameter->next;
type_argument = type_argument->next;
}
return result;
}
static bool match_bind_typevariables(bind_typevariables_type_t *variant_type,
type_t *concrete_type,
const source_position_t source_position,
bool report_errors)
{
if (concrete_type->kind != TYPE_BIND_TYPEVARIABLES) {
if (report_errors)
match_error((type_t*) variant_type, concrete_type, source_position);
return false;
}
bind_typevariables_type_t *bind_typevariables
= (bind_typevariables_type_t*) concrete_type;
compound_type_t *polymorphic_type
= bind_typevariables->polymorphic_type;
if (polymorphic_type != variant_type->polymorphic_type) {
if (report_errors)
match_error((type_t*) variant_type, concrete_type, source_position);
return false;
}
type_argument_t *argument1 = variant_type->type_arguments;
type_argument_t *argument2 = bind_typevariables->type_arguments;
bool result = true;
while (argument1 != NULL) {
assert(argument2 != NULL);
if (!match_variant_to_concrete_type(argument1->type,
argument2->type, source_position,
report_errors))
result = false;
argument1 = argument1->next;
argument2 = argument2->next;
}
assert(argument2 == NULL);
return result;
}
bool match_variant_to_concrete_type(type_t *variant_type,
type_t *concrete_type,
const source_position_t source_position,
bool report_errors)
{
type_reference_t *type_ref;
type_variable_t *type_var;
pointer_type_t *pointer_type_1;
pointer_type_t *pointer_type_2;
function_type_t *function_type_1;
function_type_t *function_type_2;
assert(type_valid(variant_type));
assert(type_valid(concrete_type));
variant_type = skip_typeref(variant_type);
concrete_type = skip_typeref(concrete_type);
switch (variant_type->kind) {
case TYPE_REFERENCE_TYPE_VARIABLE:
type_ref = (type_reference_t*) variant_type;
type_var = type_ref->type_variable;
return matched_type_variable(type_var, concrete_type, source_position,
report_errors);
case TYPE_VOID:
case TYPE_ATOMIC:
if (concrete_type != variant_type) {
if (report_errors)
match_error(variant_type, concrete_type, source_position);
return false;
}
return true;
case TYPE_COMPOUND_STRUCT:
case TYPE_COMPOUND_UNION:
return match_compound_type((compound_type_t*) variant_type,
concrete_type, source_position,
report_errors);
case TYPE_POINTER:
if (concrete_type->kind != TYPE_POINTER) {
if (report_errors)
match_error(variant_type, concrete_type, source_position);
return false;
}
pointer_type_1 = (pointer_type_t*) variant_type;
pointer_type_2 = (pointer_type_t*) concrete_type;
return match_variant_to_concrete_type(pointer_type_1->points_to,
pointer_type_2->points_to,
source_position,
report_errors);
case TYPE_FUNCTION:
if (concrete_type->kind != TYPE_FUNCTION) {
if (report_errors)
match_error(variant_type, concrete_type, source_position);
return false;
}
function_type_1 = (function_type_t*) variant_type;
function_type_2 = (function_type_t*) concrete_type;
bool result = match_variant_to_concrete_type(function_type_1->result_type,
function_type_2->result_type,
source_position,
report_errors);
function_parameter_type_t *param1 = function_type_1->parameter_types;
function_parameter_type_t *param2 = function_type_2->parameter_types;
while (param1 != NULL && param2 != NULL) {
if (!match_variant_to_concrete_type(param1->type, param2->type,
source_position, report_errors))
result = false;
param1 = param1->next;
param2 = param2->next;
}
if (param1 != NULL || param2 != NULL) {
if (report_errors)
match_error(variant_type, concrete_type, source_position);
return false;
}
return result;
case TYPE_BIND_TYPEVARIABLES:
return match_bind_typevariables(
(bind_typevariables_type_t*) variant_type,
concrete_type, source_position, report_errors);
case TYPE_ARRAY:
panic("TODO");
case TYPE_ERROR:
return false;
case TYPE_TYPEOF:
case TYPE_REFERENCE:
panic("type reference not resolved in match variant to concrete type");
case TYPE_INVALID:
panic("invalid type in match variant to concrete type");
}
panic("unknown type in match variant to concrete type");
}