-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathcodegen.cc
More file actions
141 lines (115 loc) · 5.2 KB
/
Copy pathcodegen.cc
File metadata and controls
141 lines (115 loc) · 5.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
#include <sourcemeta/blaze/alterschema.h>
#include <sourcemeta/blaze/bundle.h>
#include <sourcemeta/blaze/codegen.h>
#include <sourcemeta/blaze/frame.h>
#include <algorithm> // std::ranges::sort
#include <cassert> // assert
#include <unordered_set> // std::unordered_set
#include "codegen_default_compiler.h"
namespace {
auto is_validation_subschema(
const sourcemeta::blaze::SchemaFrame &frame,
const sourcemeta::blaze::SchemaFrame::Location &location,
const sourcemeta::blaze::SchemaWalker &walker,
const sourcemeta::blaze::SchemaResolver &resolver) -> bool {
if (!location.parent.has_value()) {
return false;
}
const auto &parent{location.parent.value()};
if (parent.size() >= location.pointer.size()) {
return false;
}
const auto &keyword_token{location.pointer.at(parent.size())};
if (!keyword_token.is_property()) {
return false;
}
const auto parent_location{frame.traverse(parent)};
if (!parent_location.has_value()) {
return false;
}
const auto vocabularies{
frame.vocabularies(parent_location.value().get(), resolver)};
const auto &walker_result{walker(keyword_token.to_property(), vocabularies)};
using Type = sourcemeta::blaze::SchemaKeywordType;
if (walker_result.type == Type::ApplicatorValueTraverseAnyPropertyKey ||
walker_result.type == Type::ApplicatorValueTraverseAnyItem) {
return true;
}
return is_validation_subschema(frame, parent_location.value().get(), walker,
resolver);
}
} // anonymous namespace
namespace sourcemeta::blaze {
auto compile(const sourcemeta::core::JSON &input,
const sourcemeta::blaze::SchemaWalker &walker,
const sourcemeta::blaze::SchemaResolver &resolver,
const CodegenCompiler &compiler,
const std::string_view default_dialect,
const std::string_view default_id) -> CodegenIRResult {
// --------------------------------------------------------------------------
// (1) Bundle the schema to resolve external references
// --------------------------------------------------------------------------
auto schema{sourcemeta::blaze::bundle(
input, walker, resolver, sourcemeta::blaze::BundleMode::References,
default_dialect, default_id)};
// --------------------------------------------------------------------------
// (2) Canonicalize the schema for easier analysis
// --------------------------------------------------------------------------
sourcemeta::blaze::SchemaTransformer canonicalizer;
sourcemeta::blaze::add(canonicalizer,
sourcemeta::blaze::AlterSchemaMode::Canonicalizer);
[[maybe_unused]] const auto canonicalized{canonicalizer.apply(
schema, walker, resolver,
[](const auto &, const auto, const auto, const auto &,
[[maybe_unused]] const auto applied) { assert(applied); },
default_dialect, default_id)};
assert(canonicalized.first);
// --------------------------------------------------------------------------
// (3) Frame the resulting schema with instance location information
// --------------------------------------------------------------------------
sourcemeta::blaze::SchemaFrame frame{
sourcemeta::blaze::SchemaFrame::Mode::References};
frame.analyse(schema, walker, resolver, default_dialect, default_id);
// --------------------------------------------------------------------------
// (4) Convert every subschema into a code generation object
// --------------------------------------------------------------------------
std::unordered_set<sourcemeta::core::WeakPointer,
sourcemeta::core::WeakPointer::Hasher,
sourcemeta::core::WeakPointer::Comparator>
visited;
CodegenIRResult result;
for (const auto &[key, location] : frame.locations()) {
if (location.type !=
sourcemeta::blaze::SchemaFrame::LocationType::Resource &&
location.type !=
sourcemeta::blaze::SchemaFrame::LocationType::Subschema) {
continue;
}
// Framing may report resource twice or more given default identifiers and
// nested resources
const auto [visited_iterator, inserted] = visited.insert(location.pointer);
if (!inserted) {
continue;
}
// Skip subschemas under validation-only keywords that do not contribute
// to the type structure (like `contains`)
if (is_validation_subschema(frame, location, walker, resolver)) {
continue;
}
const auto &subschema{sourcemeta::core::get(schema, location.pointer)};
result.push_back(compiler(schema, frame, location, resolver, subschema));
}
// --------------------------------------------------------------------------
// (5) Sort entries so that dependencies come before dependents
// --------------------------------------------------------------------------
std::ranges::sort(
result,
[](const CodegenIREntity &left, const CodegenIREntity &right) -> bool {
return std::visit([](const auto &entry) { return entry.pointer; },
right) <
std::visit([](const auto &entry) { return entry.pointer; },
left);
});
return result;
}
} // namespace sourcemeta::blaze