Blaze 0.0.1
The ultra high-performance JSON Schema evaluator
Loading...
Searching...
No Matches
compiler.h
1#ifndef SOURCEMETA_BLAZE_COMPILER_COMPILE_H_
2#define SOURCEMETA_BLAZE_COMPILER_COMPILE_H_
3
4#ifndef SOURCEMETA_BLAZE_COMPILER_EXPORT
5#include <sourcemeta/blaze/compiler_export.h>
6#endif
7
8#include <sourcemeta/blaze/compiler_error.h>
9
10#include <sourcemeta/blaze/evaluator.h>
11
12#include <sourcemeta/core/json.h>
13#include <sourcemeta/core/jsonpointer.h>
14#include <sourcemeta/core/jsonschema.h>
15#include <sourcemeta/core/uri.h>
16
17#include <cassert> // assert
18#include <cstddef> // std::size_t
19#include <cstdint> // std::uint8_t, std::uint64_t
20#include <functional> // std::function
21#include <limits> // std::numeric_limits
22#include <map> // std::map
23#include <optional> // std::optional, std::nullopt
24#include <set> // std::set
25#include <string> // std::string
26#include <string_view> // std::string_view
27#include <tuple> // std::tuple
28#include <unordered_map> // std::unordered_map
29#include <unordered_set> // std::unordered_set
30#include <vector> // std::vector
31
35
36namespace sourcemeta::blaze {
37
42 // NOLINTBEGIN(cppcoreguidelines-avoid-const-or-ref-data-members)
44 const sourcemeta::core::WeakPointer &relative_pointer;
46 const sourcemeta::core::JSON &schema;
48 const sourcemeta::core::SchemaVocabularies &vocabularies;
50 const sourcemeta::core::URI &base;
51 // NOLINTEND(cppcoreguidelines-avoid-const-or-ref-data-members)
54};
55
60 // NOLINTBEGIN(cppcoreguidelines-avoid-const-or-ref-data-members)
62 const sourcemeta::core::JSON::String &keyword;
64 const sourcemeta::core::JSON *keyword_value{nullptr};
66 const sourcemeta::core::WeakPointer &base_schema_location;
68 const sourcemeta::core::WeakPointer &base_instance_location;
69 // NOLINTEND(cppcoreguidelines-avoid-const-or-ref-data-members)
70
73 [[nodiscard]] auto value() const -> const sourcemeta::core::JSON & {
74 assert(this->keyword_value != nullptr);
75 return *this->keyword_value;
76 }
77};
78
79#if !defined(DOXYGEN)
80struct Context;
81struct KeywordContext;
82#endif
83
88using Compiler =
89 // TODO(C++23): Use std::move_only_function when available in libc++
90 std::function<Instructions(const KeywordContext &, const Instructions &)>;
91
94enum class Mode : std::uint8_t {
99};
100
106class SOURCEMETA_BLAZE_COMPILER_EXPORT InstructionExtras {
107public:
108 explicit InstructionExtras(const std::uint64_t limit) : limit_{limit} {}
109
111 auto push_back(InstructionExtra &&entry) -> void {
112 if (this->entries_.size() >= this->limit_) [[unlikely]] {
113 throw CompilerInstructionLimitError{this->limit_};
114 }
115
116 this->entries_.push_back(std::move(entry));
117 }
118
119 [[nodiscard]] auto size() const noexcept -> std::size_t {
120 return this->entries_.size();
121 }
122
123 [[nodiscard]] auto operator[](const std::size_t index) const noexcept
124 -> const InstructionExtra & {
125 return this->entries_[index];
126 }
127
128 [[nodiscard]] auto operator[](const std::size_t index) noexcept
129 -> InstructionExtra & {
130 return this->entries_[index];
131 }
132
134 [[nodiscard]] auto release() && -> std::vector<InstructionExtra> {
135 return std::move(this->entries_);
136 }
137
138private:
139// Exporting symbols that depends on the standard C++ library is considered
140// safe.
141// https://learn.microsoft.com/en-us/cpp/error-messages/compiler-warnings/compiler-warning-level-2-c4275?view=msvc-170&redirectedfrom=MSDN
142#if defined(_MSC_VER)
143#pragma warning(disable : 4251 4275)
144#endif
145 std::vector<InstructionExtra> entries_;
146#if defined(_MSC_VER)
147#pragma warning(default : 4251 4275)
148#endif
149 std::uint64_t limit_;
150};
151
154struct Tweaks {
160 std::size_t target_inline_threshold{50};
162 bool format_assertion{false};
166 std::optional<std::unordered_set<sourcemeta::core::JSON::StringView>>
172 std::uint64_t max_instructions{std::numeric_limits<std::uint64_t>::max()};
177 std::uint64_t max_depth{std::numeric_limits<std::uint64_t>::max()};
178};
179
184struct Context {
188 std::set<sourcemeta::core::WeakPointer> static_dependencies;
191 std::set<sourcemeta::core::WeakPointer> dynamic_dependencies;
194 bool unresolved{false};
195 };
196
197 // NOLINTBEGIN(cppcoreguidelines-avoid-const-or-ref-data-members)
199 const sourcemeta::core::JSON &root;
201 const sourcemeta::core::SchemaFrame &frame;
203 const std::vector<std::string> resources;
205 const sourcemeta::core::SchemaWalker &walker;
207 const sourcemeta::core::SchemaResolver &resolver;
211 const Mode mode;
216 const std::vector<std::string> anchors;
220 const std::vector<std::size_t> resource_bindings;
224 const std::map<sourcemeta::core::JSON::String, UnevaluatedEntry> unevaluated;
226 // TODO: This should go away if we start properly compiling
227 // `unevaluatedItems` like we compile `unevaluatedProperties`, as the
228 // dependencies of the former are not traced, so any occurrence of it
229 // anywhere forces every enclosing instruction to keep track of evaluation
234 const std::map<
235 std::tuple<sourcemeta::core::SchemaReferenceType, std::string_view, bool>,
236 std::pair<std::size_t, const sourcemeta::core::WeakPointer *>>
239 std::uint64_t &depth;
243 std::vector<sourcemeta::core::SchemaVocabularies::URI> &vocabularies;
244 // NOLINTEND(cppcoreguidelines-avoid-const-or-ref-data-members)
245};
246
250const sourcemeta::core::JSON::String EMPTY_KEYWORD;
251
256 // NOLINTBEGIN(cppcoreguidelines-avoid-const-or-ref-data-members)
261 // NOLINTEND(cppcoreguidelines-avoid-const-or-ref-data-members)
265
267 [[nodiscard]] auto keyword() const -> const sourcemeta::core::JSON::String & {
268 return this->target.keyword;
269 }
270
272 [[nodiscard]] auto value() const -> const sourcemeta::core::JSON & {
273 return this->target.value();
274 }
275
277 [[nodiscard]] auto schema() const -> const sourcemeta::core::JSON & {
278 return this->local.schema;
279 }
280
282 [[nodiscard]] auto vocabularies() const
283 -> const sourcemeta::core::SchemaVocabularies & {
284 return this->local.vocabularies;
285 }
286
288 [[nodiscard]] auto base() const -> const sourcemeta::core::URI & {
289 return this->local.base;
290 }
291
293 [[nodiscard]] auto mode() const -> Mode { return this->global.mode; }
294
297 [[nodiscard]] auto relative() const -> KeywordContext {
298 return {
299 .global = this->global,
300 .local = this->local,
301 .target = {.keyword = EMPTY_KEYWORD,
302 .base_schema_location = sourcemeta::core::EMPTY_WEAK_POINTER,
303 .base_instance_location =
304 sourcemeta::core::EMPTY_WEAK_POINTER}};
305 }
306};
307
311auto SOURCEMETA_BLAZE_COMPILER_EXPORT default_schema_compiler(
312 const KeywordContext &, const Instructions &) -> Instructions;
313
344auto SOURCEMETA_BLAZE_COMPILER_EXPORT
345compile(const sourcemeta::core::JSON &schema,
346 const sourcemeta::core::SchemaWalker &walker,
347 const sourcemeta::core::SchemaResolver &resolver,
348 const Compiler &compiler, const Mode mode = Mode::FastValidation,
349 const std::string_view default_dialect = "",
350 const std::string_view default_id = "",
351 const std::string_view entrypoint = "",
352 const std::optional<Tweaks> &tweaks = std::nullopt,
353 std::uint64_t max_locations = std::numeric_limits<std::uint64_t>::max())
354 -> Template;
355
365auto SOURCEMETA_BLAZE_COMPILER_EXPORT compile(
366 const sourcemeta::core::JSON &schema,
367 const sourcemeta::core::SchemaWalker &walker,
368 const sourcemeta::core::SchemaResolver &resolver, const Compiler &compiler,
369 const sourcemeta::core::SchemaFrame &frame,
370 const std::string_view entrypoint, const Mode mode = Mode::FastValidation,
371 const std::optional<Tweaks> &tweaks = std::nullopt) -> Template;
372
381auto SOURCEMETA_BLAZE_COMPILER_EXPORT
382compile(const KeywordContext &context,
383 const sourcemeta::core::WeakPointer &schema_suffix,
384 const sourcemeta::core::WeakPointer &instance_suffix =
385 sourcemeta::core::EMPTY_WEAK_POINTER,
386 std::optional<std::string_view> uri = std::nullopt) -> Instructions;
387
390auto SOURCEMETA_BLAZE_COMPILER_EXPORT to_json(const Template &schema_template)
391 -> sourcemeta::core::JSON;
392
393} // namespace sourcemeta::blaze
394
395#endif
auto relative() const -> KeywordContext
Definition compiler.h:297
const std::vector< std::size_t > resource_bindings
Definition compiler.h:220
auto vocabularies() const -> const sourcemeta::core::SchemaVocabularies &
The vocabularies in force where the keyword is declared.
Definition compiler.h:282
auto value() const -> const sourcemeta::core::JSON &
What the keyword is set to.
Definition compiler.h:272
const Mode mode
The mode of the schema compiler.
Definition compiler.h:211
const sourcemeta::core::JSON * keyword_value
The value of the keyword, null in a context that names none.
Definition compiler.h:64
const Context & global
What holds for the entire compilation.
Definition compiler.h:258
InstructionExtras & extra
Accumulator for instruction extra data during compilation.
Definition compiler.h:241
bool format_assertion
When set, force format to be compiled as an assertion.
Definition compiler.h:162
bool properties_always_unroll
Always unroll properties in a logical AND operation.
Definition compiler.h:156
auto value() const -> const sourcemeta::core::JSON &
Definition compiler.h:73
const sourcemeta::core::WeakPointer & relative_pointer
The schema location relative to the base URI.
Definition compiler.h:44
const sourcemeta::core::WeakPointer & base_schema_location
The schema base keyword path.
Definition compiler.h:66
const sourcemeta::core::SchemaFrame & frame
The reference frame of the entire schema.
Definition compiler.h:201
auto keyword() const -> const sourcemeta::core::JSON::String &
The keyword itself.
Definition compiler.h:267
std::vector< sourcemeta::core::SchemaVocabularies::URI > & vocabularies
Accumulator for the vocabularies that instructions refer to.
Definition compiler.h:243
const sourcemeta::core::SchemaWalker & walker
The schema walker in use.
Definition compiler.h:205
auto schema() const -> const sourcemeta::core::JSON &
The subschema that declares the keyword.
Definition compiler.h:277
DynamicContext target
Definition compiler.h:264
const sourcemeta::core::JSON & root
The root schema resource.
Definition compiler.h:199
bool properties_reorder
Attempt to re-order properties subschemas to evaluate cheaper ones first.
Definition compiler.h:158
std::uint64_t max_instructions
Definition compiler.h:172
const sourcemeta::core::WeakPointer & base_instance_location
The base instance location that the keyword must be evaluated to.
Definition compiler.h:68
std::size_t target_inline_threshold
Inline jump targets with fewer instructions than this threshold.
Definition compiler.h:160
const sourcemeta::core::SchemaVocabularies & vocabularies
The schema vocabularies in use.
Definition compiler.h:48
const std::map< std::tuple< sourcemeta::core::SchemaReferenceType, std::string_view, bool >, std::pair< std::size_t, const sourcemeta::core::WeakPointer * > > targets
All possible reference targets (key includes is_property_name context).
Definition compiler.h:237
const SchemaContext & local
The subschema that the keyword belongs to.
Definition compiler.h:260
const bool uses_dynamic_scopes
Whether the schema makes use of dynamic scoping.
Definition compiler.h:213
auto release() &&-> std::vector< InstructionExtra >
Hand the accumulated entries over to the template that will own them.
Definition compiler.h:134
const sourcemeta::core::JSON & schema
The current subschema.
Definition compiler.h:46
std::uint64_t & depth
How deep into the schema compilation currently is.
Definition compiler.h:239
const sourcemeta::core::JSON::String & keyword
The schema keyword.
Definition compiler.h:62
const std::vector< std::string > resources
The set of all schema resources in the schema without duplicates.
Definition compiler.h:203
const std::vector< std::string > anchors
Definition compiler.h:216
const bool defines_unevaluated_items
Whether the schema declares unevaluatedItems anywhere at all.
Definition compiler.h:230
auto push_back(InstructionExtra &&entry) -> void
Make room for another instruction, throwing once out of room.
Definition compiler.h:111
std::uint64_t max_depth
Definition compiler.h:177
auto mode() const -> Mode
The mode the whole schema is being compiled in.
Definition compiler.h:293
const sourcemeta::core::SchemaResolver & resolver
The schema resolver in use.
Definition compiler.h:207
const bool collects_annotations
Whether evaluating the schema may collect at least one annotation.
Definition compiler.h:222
const sourcemeta::core::URI & base
The schema base URI.
Definition compiler.h:50
const Tweaks tweaks
The set of tweaks for the compiler.
Definition compiler.h:232
auto base() const -> const sourcemeta::core::URI &
The base URI of the subschema that declares the keyword.
Definition compiler.h:288
std::optional< std::unordered_set< sourcemeta::core::JSON::StringView > > annotations
Definition compiler.h:167
bool is_property_name
Whether the current schema targets a property name.
Definition compiler.h:53
const Compiler & compiler
The schema compiler in use.
Definition compiler.h:209
const std::map< sourcemeta::core::JSON::String, UnevaluatedEntry > unevaluated
The flattened set of unevaluated cases in the schema by absolute URI.
Definition compiler.h:224
Definition compiler.h:106
const sourcemeta::core::JSON::String EMPTY_KEYWORD
Definition compiler.h:250
auto default_schema_compiler(const KeywordContext &, const Instructions &) -> Instructions
auto to_json(const Template &schema_template) -> sourcemeta::core::JSON
std::function< Instructions(const KeywordContext &, const Instructions &)> Compiler
Definition compiler.h:88
Definition compiler.h:184
Definition compiler.h:59
Definition compiler.h:255
Definition compiler.h:41
Definition compiler.h:154
std::vector< Instruction > Instructions
Definition evaluator_instruction.h:347
Mode
Definition compiler.h:94
@ FastValidation
Attempt to get to a boolean result as fast as possible.
Definition compiler.h:96
@ Exhaustive
Perform exhaustive evaluation, including annotations.
Definition compiler.h:98
Definition evaluator_instruction.h:351
Definition evaluator.h:35
An unevaluated case in the schema and the dependencies it reaches.
Definition compiler.h:186
std::set< sourcemeta::core::WeakPointer > dynamic_dependencies
Definition compiler.h:191
std::set< sourcemeta::core::WeakPointer > static_dependencies
The absolute pointers of the static keyword dependencies.
Definition compiler.h:188
bool unresolved
Definition compiler.h:194