-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinterface.verify.cpp
More file actions
429 lines (377 loc) · 17.2 KB
/
interface.verify.cpp
File metadata and controls
429 lines (377 loc) · 17.2 KB
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
#define NOMINMAX
#define WIN32_LEAN_AND_MEAN
#include "interface.hpp"
#include "module.hpp"
#include "diagnostics.hpp"
#include "string_helpers.hpp"
#include "sema/lookup.hpp"
#include "sema/function_arity.hpp"
#include <unordered_map>
namespace doir::verify {
diagnose::source_location get_location(module& mod, std::string_view s) {
size_t start;
if(contains(mod.source, s))
start = mod.source.data() - s.data();
else start = mod.source.find(s);
if(start == std::string_view::npos)
return {mod.working_file.value_or("<unknown>"), 0, 0};
return {mod.working_file.value_or("<unknown>"), start, start + s.length()};
}
bool entity_exists(diagnose::manager &diagnostics, module &mod, ecrs::entity_t subtree) {
if( !(subtree <= mod.entity_count()) ) {
throw std::runtime_error("TODO: invalid entity error");
return false;
}
if(mod.freelist && fp::contains(mod.freelist.begin(), mod.freelist.end(), subtree)) {
throw std::runtime_error("TODO: entity removed error");
return false;
}
return true;
}
bool identifier_structure(diagnose::manager &diagnostics, module &mod, interned_string ident, bool allow_builtins /*= true*/) {
using namespace std::string_literals;
std::string invalid_message = "";
size_t invalid_offset;
if(ident[0] == '%') {
if(ident.size() == 1) {
invalid_message = "A "s + doir::ansi::info + "%" + diagnose::ansi::reset + " in an identifier must by followed by a number";
invalid_offset = 0;
}
for(size_t i = 1; i < ident.size(); ++i)
if(!std::isdigit(ident[i])) {
invalid_message = "Only numbers can follow a "s + doir::ansi::info + "%" + diagnose::ansi::reset;
invalid_offset = i;
break;
}
}
if(!allow_builtins) {
std::unordered_set<std::string_view> builtins = DOIR_BUILTIN_NAMES;
if(builtins.contains(ident)) {
invalid_message = "Reserved identifier "s + doir::ansi::type + std::string(ident) + diagnose::ansi::reset + " not allowed here";
invalid_offset = 0;
}
}
if(!invalid_message.empty()) {
auto& diag = diagnostics.push(generate_diagnostic(diagnostic_type::InvalidIdentifier, get_location(mod, ident), mod.source, *mod.working_file));
diag.push_annotation({
.position = {diag.location.start.line, diag.location.start.column + invalid_offset},
.message = invalid_message,
.color = doir::ansi::info,
});
return false;
}
return true;
}
bool verify_lookup_structure(diagnose::manager &diagnostics, module &mod, const lookup::lookup& l) {
if(l.resolved()) {
if(!verify::entity_exists(diagnostics, mod, l.entity())) return false;
} else if(!verify::identifier_structure(diagnostics, mod, l.name())) return false;
return true;
}
bool structure(diagnose::manager& diagnostics, module& mod, ecrs::entity_t subtree, bool top_level /* = true */, ecrs::entity_t builtin_end /*= ecrs::invalid_entitt */) {
bool valid = false;
if(builtin_end == ecrs::invalid_entity) {
auto compiler = lookup::resolve(mod, mod.interner.intern("compiler"), subtree);
if(compiler == ecrs::invalid_entity) builtin_end = 1;
else {
auto& b = mod.get_component<doir::block>(compiler);
auto max = *std::max_element(b.related.begin(), b.related.end());
builtin_end = std::max(compiler, max);
}
}
if(mod.has_component<type_of>(subtree) || mod.has_component<lookup::type_of>(subtree)) {
valid = true;
lookup::type_of t = mod.has_component<type_of>(subtree)
? lookup::type_of(mod.get_component<type_of>(subtree).related[0])
: mod.get_component<lookup::type_of>(subtree);
if(!verify_lookup_structure(diagnostics, mod, t)) valid = false;
if(mod.has_component<doir::pointer>(subtree)
|| mod.has_component<doir::type_definition>(subtree)
|| mod.has_component<doir::alias>(subtree)
|| mod.has_component<doir::lookup::lookup>(subtree)
|| mod.has_component<doir::lookup::alias>(subtree)
){
throw std::runtime_error("TODO: Invalid component");
valid = false;
}
bool valueless = mod.has_component<doir::flags>(subtree) && mod.get_component<doir::flags>(subtree).valueless_set();
bool number = mod.has_component<doir::number>(subtree);
bool string = mod.has_component<doir::string>(subtree);
bool call = mod.has_component<doir::call>(subtree);
bool call_lookup = mod.has_component<doir::lookup::call>(subtree);
bool function_def = mod.has_component<doir::function_return_type>(subtree);
bool function_def_lookup = mod.has_component<doir::lookup::function_return_type>(subtree);
bool block = mod.has_component<doir::block>(subtree);
// if( (call || call_lookup || function_def || function_def_lookup || block) && mod.has_component<doir::function_parameter>(subtree)) {
// // TODO: Should we maintain this rule?
// throw std::runtime_error("TODO: Parameters can only have a string, a number, or nothing as a default value");
// valid = false;
// }
size_t count = valueless + number + string + call + call_lookup + function_def + function_def_lookup + block;
switch(count) {
break; case 0:
throw std::runtime_error("TODO: Value required error");
valid = false;
break; case 1:
// Do Nothing we are good :)
break; case 2:
if( !((function_def && block) || (function_def_lookup && block)) ) {
throw std::runtime_error("TODO: Only one value is allowed error");
valid = false;
}
break; default:
throw std::runtime_error("TODO: Only one value is allowed error");
valid = false;
}
auto flags = mod.has_component<doir::flags>(subtree)
? std::optional<doir::flags>(mod.get_component<doir::flags>(subtree))
: std::optional<doir::flags>{};
if( !(call || call_lookup || function_def || function_def_lookup) && flags) {
if((!block && flags->flatten_set()) || flags->inline_set() || flags->pure_set() || flags->tail_set()) {
throw std::runtime_error("TODO: Invalid flags");
valid = false;
}
}
if(flags && flags->constant_set()) {
throw std::runtime_error("TODO: Only pointer types can be marked constant");
valid = false;
}
if(call || call_lookup) {
doir::lookup::lookup call = mod.has_component<doir::call>(subtree)
? doir::lookup::lookup(mod.get_component<doir::call>(subtree).related[0])
: doir::lookup::lookup(mod.get_component<doir::lookup::call>(subtree));
// if(call.resolved() && !verify::structure(diagnostics, mod, call.entity(), false, builtin_end)) valid = false;
auto inputs = mod.has_component<doir::function_inputs>(subtree)
? doir::lookup::function_inputs::to_lookup(mod.get_component<doir::function_inputs>(subtree))
: mod.get_component<doir::lookup::function_inputs>(subtree);
// for(auto& i: inputs)
// if(i.resolved() && !verify::structure(diagnostics, mod, i.entity(), false, builtin_end)) valid = false;
} else if(function_def || function_def_lookup) {
if(!t.resolved()) {
throw std::runtime_error("TODO: function types can't be looked up");
return false;
}
// if(!verify::structure(diagnostics, mod, t.entity(), false, builtin_end)) valid = false;
auto ft = doir::sema::validate::resolve_type_modifications(mod, t.entity());
auto inputs = mod.has_component<doir::function_inputs>(ft)
? doir::lookup::function_inputs::to_lookup(mod.get_component<doir::function_inputs>(ft))
: mod.get_component<doir::lookup::function_inputs>(ft);
if(mod.has_component<doir::function_parameter_names>(subtree)) {
auto& names = mod.get_component<doir::function_parameter_names>(subtree);
if(names.size() != inputs.size()) {
throw std::runtime_error("TODO: The number of parameter names must match the number of parameters");
valid = false;
}
}
if(mod.has_component<doir::block>(subtree)) {
auto associated_parameters = inputs.associated_parameters(mod, mod.get_component<doir::block>(subtree));
if(associated_parameters.size() != inputs.size()) {
throw std::runtime_error("TODO: A different number of parameters are defined than declared.");
valid = false;
}
if(mod.has_component<doir::function_parameter_names>(subtree)) {
auto& names = mod.get_component<doir::function_parameter_names>(subtree);
for(size_t i = 0; i < associated_parameters.size(); ++i)
if(mod.has_component<doir::name>(associated_parameters[i]) && names[i] != mod.get_component<doir::name>(associated_parameters[i])) {
throw std::runtime_error("TODO: Parameter names differ");
valid = false;
}
}
}
}
} else if(mod.has_component<type_definition>(subtree)) {
valid = true;
if( !(mod.has_component<doir::block>(subtree)
|| mod.has_component<doir::function_return_type>(subtree)
|| mod.has_component<doir::lookup::function_return_type>(subtree) // TODO: Valid?
|| mod.has_component<doir::pointer>(subtree)
// || mod.has_component<doir::call>(root)
// || mod.has_component<doir::lookup::call>(root)
) ) {
std::cout << subtree << std::endl;
throw std::runtime_error("Types are required to have a `block`, function types are required to have a `function_inputs`, and pointers/arrays must have `pointer`");
valid = false;
}
if(mod.has_component<doir::function_parameter>(subtree)
|| mod.has_component<doir::alias>(subtree)
|| mod.has_component<doir::type_of>(subtree)
|| mod.has_component<doir::number>(subtree)
|| mod.has_component<doir::string>(subtree)
|| mod.has_component<doir::call>(subtree)
|| mod.has_component<doir::lookup::lookup>(subtree)
// || mod.has_component<doir::lookup::block>(root)
|| mod.has_component<doir::lookup::alias>(subtree)
|| mod.has_component<doir::lookup::type_of>(subtree)
|| mod.has_component<doir::lookup::call>(subtree)
){
throw std::runtime_error("TODO: Invalid component attached");
valid = false;
}
if(mod.has_component<doir::function_inputs>(subtree)) {
auto inputs = mod.has_component<doir::function_inputs>(subtree)
? doir::lookup::function_inputs::to_lookup(mod.get_component<doir::function_inputs>(subtree))
: mod.get_component<doir::lookup::function_inputs>(subtree);
std::optional<doir::lookup::function_return_type> return_type = {};
if(mod.has_component<doir::function_return_type>(subtree))
return_type = {mod.get_component<doir::function_return_type>(subtree).related[0]};
else if(mod.has_component<doir::lookup::function_return_type>(subtree))
return_type = mod.get_component<doir::lookup::function_return_type>(subtree);
if(mod.has_component<doir::function_parameter_names>(subtree)) {
auto& names = mod.get_component<doir::function_parameter_names>(subtree);
if(names.size() != inputs.size()) {
throw std::runtime_error("TODO: The number of parameter names must match the number of parameters");
}
}
// if(return_type && return_type->resolved() && !verify::structure(diagnostics, mod, return_type->entity(), false, builtin_end))
// valid = false;
// for(auto& i: inputs)
// if(i.resolved() && !verify::structure(diagnostics, mod, i.entity(), false, builtin_end)) valid = false;
}
if(mod.has_component<doir::pointer>(subtree)) {
static auto type = doir::lookup::resolve(mod, "type", subtree);
auto base = mod.get_component<doir::pointer>(subtree).related[0];
if( !(mod.has_component<doir::type_definition>(base)
|| (mod.has_component<doir::type_of>(base) && mod.get_component<doir::type_of>(base).related[0] == type))
) {
throw std::runtime_error("TODO: Pointers must have a type as their base");
valid = false;
}
}
if(mod.has_component<doir::flags>(subtree)) {
auto flags = mod.get_component<doir::flags>(subtree);
if(flags.valueless_set() || flags.namespace_set()) {
throw std::runtime_error("TODO: Invalid flags");
valid = false;
}
// TODO: What flags are valid?
}
} else if(mod.has_component<doir::flags>(subtree) && mod.get_component<doir::flags>(subtree).namespace_set()) {
valid = true;
if(!mod.has_component<doir::block>(subtree)) {
throw std::runtime_error("Namespaces are required to have a block");
valid = false;
}
if(mod.has_component<doir::flags>(subtree)) {
auto flags = mod.get_component<doir::flags>(subtree);
flags.as_underlying() &= ~doir::flags::Namespace;
if( !(flags.flags == doir::flags::None || flags.flags == doir::flags::Export) ) {
throw std::runtime_error("Invalid flags");
valid = false;
}
}
if(mod.has_component<doir::pointer>(subtree)
|| mod.has_component<doir::function_return_type>(subtree)
|| mod.has_component<doir::function_inputs>(subtree)
|| mod.has_component<doir::function_parameter>(subtree)
|| mod.has_component<doir::type_definition>(subtree)
|| mod.has_component<doir::alias>(subtree)
|| mod.has_component<doir::type_of>(subtree)
|| mod.has_component<doir::number>(subtree)
|| mod.has_component<doir::string>(subtree)
|| mod.has_component<doir::call>(subtree)
|| mod.has_component<doir::lookup::lookup>(subtree)
|| mod.has_component<doir::lookup::function_return_type>(subtree)
|| mod.has_component<doir::lookup::function_inputs>(subtree)
// || mod.has_component<doir::lookup::block>(root)
|| mod.has_component<doir::lookup::alias>(subtree)
|| mod.has_component<doir::lookup::type_of>(subtree)
|| mod.has_component<doir::lookup::call>(subtree)
){
throw std::runtime_error("TODO: Invalid component attached");
valid = false;
}
if(mod.has_component<doir::flags>(subtree)) {
auto flags = mod.get_component<doir::flags>(subtree);
if(flags.valueless_set()) {
throw std::runtime_error("TODO: Invalid flags");
valid = false;
}
// TODO: What flags are valid?
}
} else if(mod.has_component<alias>(subtree) || mod.has_component<doir::lookup::alias>(subtree)) {
if(mod.has_component<doir::pointer>(subtree)
|| mod.has_component<doir::function_return_type>(subtree)
|| mod.has_component<doir::function_inputs>(subtree)
|| mod.has_component<doir::function_parameter>(subtree)
|| mod.has_component<doir::block>(subtree)
|| mod.has_component<doir::type_definition>(subtree)
|| mod.has_component<doir::type_of>(subtree)
|| mod.has_component<doir::number>(subtree)
|| mod.has_component<doir::string>(subtree)
|| mod.has_component<doir::call>(subtree)
|| mod.has_component<doir::lookup::lookup>(subtree)
|| mod.has_component<doir::lookup::function_return_type>(subtree)
|| mod.has_component<doir::lookup::function_inputs>(subtree)
// || mod.has_component<doir::lookup::block>(root)
|| mod.has_component<doir::lookup::type_of>(subtree)
|| mod.has_component<doir::lookup::call>(subtree)
){
throw std::runtime_error("TODO: Invalid component attached");
valid = false;
}
if(mod.has_component<doir::flags>(subtree)) {
auto flags = mod.get_component<doir::flags>(subtree).flags;
if( !(flags == doir::flags::None || flags == doir::flags::Export) ) {
throw std::runtime_error("Invalid flags");
valid = false;
}
}
// auto alias = mod.has_component<doir::alias>(subtree)
// ? lookup::alias(mod.get_component<doir::alias>(subtree).related[0])
// : mod.get_component<lookup::alias>(subtree);
// if(alias.resolved())
// return verify::structure(diagnostics, mod, alias.entity(), false, builtin_end);
} else if(mod.has_component<block>(subtree)) {
if(mod.has_component<doir::flags>(subtree)) {
auto flags = mod.get_component<doir::flags>(subtree).flags;
if( !(flags == doir::flags::None || flags == doir::flags::Flatten) ) {
throw std::runtime_error("Invalid flags");
valid = false;
}
}
if(top_level)
valid = true;
else {
throw std::runtime_error("TODO: A standalone block not as part of an assignment is only allowed at the top level!");
valid = false;
}
}
if(fp::contains(mod.freelist.begin(), mod.freelist.end(), subtree)) {
throw std::runtime_error("TODO: Referenced deleted entity");
valid = false;
}
if(mod.has_component<name>(subtree))
if(!verify::identifier_structure(diagnostics, mod, mod.get_component<name>(subtree), subtree <= builtin_end)) valid = false;
if(mod.has_component<doir::parent>(subtree)) {
auto parent = mod.get_component<doir::parent>(subtree).related[0];
if(!mod.has_component<doir::block>(parent)) {
throw std::runtime_error("TODO: Parents must point to a valid block");
valid = false;
}
auto& block = mod.get_component<doir::block>(parent);
if(std::find(block.related.begin(), block.related.end(), subtree) == block.related.end()) {
throw std::runtime_error("TODO: The parent of an entity must also list that entity in its block");
valid = false;
}
}
if(mod.has_component<doir::block>(subtree)) {
auto& related = mod.get_component<doir::block>(subtree).related;
if(related.size() != std::unordered_set<ecrs::entity_t>{related.begin(), related.end()}.size()) {
throw std::runtime_error("TODO: Duplicate elements in block");
valid = false;
}
for(auto e: related) {
if(mod.has_component<doir::parent>(e)) {
auto parent = mod.get_component<doir::parent>(e).related[0];
if(parent != subtree) {
throw std::runtime_error("TODO: Children of a block must list that block as their parent");
valid = false;
}
}
if(!verify::structure(diagnostics, mod, e, false, builtin_end))
valid = false;
}
}
return valid;
}
} // namespace doir