1#ifndef SOURCEMETA_BLAZE_COMPILER_COMPILE_H_
2#define SOURCEMETA_BLAZE_COMPILER_COMPILE_H_
4#ifndef SOURCEMETA_BLAZE_COMPILER_EXPORT
5#include <sourcemeta/blaze/compiler_export.h>
8#include <sourcemeta/blaze/compiler_error.h>
10#include <sourcemeta/blaze/evaluator.h>
12#include <sourcemeta/core/json.h>
13#include <sourcemeta/core/jsonpointer.h>
14#include <sourcemeta/core/jsonschema.h>
15#include <sourcemeta/core/uri.h>
28#include <unordered_map>
29#include <unordered_set>
36namespace sourcemeta::blaze {
53 std::map<sourcemeta::core::JSON::String, SchemaUnevaluatedEntry>;
63 const sourcemeta::core::JSON &
schema;
67 const sourcemeta::core::URI &
base;
79 const sourcemeta::core::JSON::String &
keyword;
90 [[nodiscard]]
auto value() const -> const sourcemeta::core::JSON & {
91 assert(this->keyword_value !=
nullptr);
92 return *this->keyword_value;
111enum class Mode : std::uint8_t {
123class SOURCEMETA_BLAZE_COMPILER_EXPORT InstructionExtras {
125 explicit InstructionExtras(
const std::uint64_t limit) : limit_{limit} {}
129 if (this->entries_.size() >= this->limit_) [[unlikely]] {
133 this->entries_.push_back(std::move(entry));
136 [[nodiscard]]
auto size() const noexcept -> std::
size_t {
137 return this->entries_.size();
140 [[nodiscard]]
auto operator[](
const std::size_t index)
const noexcept
141 ->
const InstructionExtra & {
142 return this->entries_[index];
145 [[nodiscard]]
auto operator[](
const std::size_t index)
noexcept
146 -> InstructionExtra & {
147 return this->entries_[index];
151 [[nodiscard]]
auto release() && -> std::vector<InstructionExtra> {
152 return std::move(this->entries_);
160#pragma warning(disable : 4251 4275)
162 std::vector<InstructionExtra> entries_;
164#pragma warning(default : 4251 4275)
166 std::uint64_t limit_;
183 std::optional<std::unordered_set<sourcemeta::core::JSON::StringView>>
194 std::uint64_t
max_depth{std::numeric_limits<std::uint64_t>::max()};
204 const sourcemeta::core::JSON &
root;
206 const sourcemeta::core::SchemaFrame &
frame;
210 const sourcemeta::core::SchemaWalker &
walker;
227 std::tuple<sourcemeta::core::SchemaReferenceType, std::string_view, bool>,
228 std::pair<std::size_t, const sourcemeta::core::WeakPointer *>>
259 [[nodiscard]]
auto keyword() const -> const sourcemeta::core::JSON::String & {
264 [[nodiscard]]
auto value() const -> const sourcemeta::core::JSON & {
265 return this->target.
value();
269 [[nodiscard]]
auto schema() const -> const sourcemeta::core::JSON & {
270 return this->local.
schema;
275 -> const sourcemeta::core::SchemaVocabularies & {
280 [[nodiscard]]
auto base() const -> const sourcemeta::core::URI & {
281 return this->local.
base;
291 .global = this->global,
292 .local = this->local,
294 .base_schema_location = sourcemeta::core::EMPTY_WEAK_POINTER,
295 .base_instance_location =
296 sourcemeta::core::EMPTY_WEAK_POINTER}};
336auto SOURCEMETA_BLAZE_COMPILER_EXPORT
337compile(
const sourcemeta::core::JSON &schema,
338 const sourcemeta::core::SchemaWalker &walker,
339 const sourcemeta::core::SchemaResolver &resolver,
341 const std::string_view default_dialect =
"",
342 const std::string_view default_id =
"",
343 const std::string_view entrypoint =
"",
344 const std::optional<Tweaks> &tweaks = std::nullopt,
345 std::uint64_t max_locations = std::numeric_limits<std::uint64_t>::max())
357auto SOURCEMETA_BLAZE_COMPILER_EXPORT compile(
358 const sourcemeta::core::JSON &schema,
359 const sourcemeta::core::SchemaWalker &walker,
360 const sourcemeta::core::SchemaResolver &resolver,
const Compiler &compiler,
361 const sourcemeta::core::SchemaFrame &frame,
363 const std::optional<Tweaks> &tweaks = std::nullopt) ->
Template;
373auto SOURCEMETA_BLAZE_COMPILER_EXPORT
375 const sourcemeta::core::WeakPointer &schema_suffix,
376 const sourcemeta::core::WeakPointer &instance_suffix =
377 sourcemeta::core::EMPTY_WEAK_POINTER,
378 std::optional<std::string_view> uri = std::nullopt) ->
Instructions;
383 -> sourcemeta::core::JSON;
auto relative() const -> KeywordContext
Definition compiler.h:289
auto vocabularies() const -> const sourcemeta::core::SchemaVocabularies &
The vocabularies in force where the keyword is declared.
Definition compiler.h:274
auto value() const -> const sourcemeta::core::JSON &
What the keyword is set to.
Definition compiler.h:264
const Mode mode
The mode of the schema compiler.
Definition compiler.h:216
const sourcemeta::core::JSON * keyword_value
The value of the keyword, null in a context that names none.
Definition compiler.h:81
const Context & global
What holds for the entire compilation.
Definition compiler.h:250
InstructionExtras & extra
Accumulator for instruction extra data during compilation.
Definition compiler.h:233
bool format_assertion
When set, force format to be compiled as an assertion.
Definition compiler.h:179
bool properties_always_unroll
Always unroll properties in a logical AND operation.
Definition compiler.h:173
auto value() const -> const sourcemeta::core::JSON &
Definition compiler.h:90
const sourcemeta::core::WeakPointer & relative_pointer
The schema location relative to the base URI.
Definition compiler.h:61
const sourcemeta::core::WeakPointer & base_schema_location
The schema base keyword path.
Definition compiler.h:83
const sourcemeta::core::SchemaFrame & frame
The reference frame of the entire schema.
Definition compiler.h:206
auto keyword() const -> const sourcemeta::core::JSON::String &
The keyword itself.
Definition compiler.h:259
std::set< sourcemeta::core::WeakPointer > dynamic_dependencies
Definition compiler.h:44
std::vector< sourcemeta::core::SchemaVocabularies::URI > & vocabularies
Accumulator for the vocabularies that instructions refer to.
Definition compiler.h:235
const SchemaUnevaluatedEntries unevaluated
The list of unevaluated entries and their dependencies.
Definition compiler.h:222
const sourcemeta::core::SchemaWalker & walker
The schema walker in use.
Definition compiler.h:210
auto schema() const -> const sourcemeta::core::JSON &
The subschema that declares the keyword.
Definition compiler.h:269
DynamicContext target
Definition compiler.h:256
const sourcemeta::core::JSON & root
The root schema resource.
Definition compiler.h:204
bool properties_reorder
Attempt to re-order properties subschemas to evaluate cheaper ones first.
Definition compiler.h:175
std::uint64_t max_instructions
Definition compiler.h:189
const sourcemeta::core::WeakPointer & base_instance_location
The base instance location that the keyword must be evaluated to.
Definition compiler.h:85
std::size_t target_inline_threshold
Inline jump targets with fewer instructions than this threshold.
Definition compiler.h:177
const sourcemeta::core::SchemaVocabularies & vocabularies
The schema vocabularies in use.
Definition compiler.h:65
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:229
const SchemaContext & local
The subschema that the keyword belongs to.
Definition compiler.h:252
const bool uses_dynamic_scopes
Whether the schema makes use of dynamic scoping.
Definition compiler.h:218
auto release() &&-> std::vector< InstructionExtra >
Hand the accumulated entries over to the template that will own them.
Definition compiler.h:151
const sourcemeta::core::JSON & schema
The current subschema.
Definition compiler.h:63
std::uint64_t & depth
How deep into the schema compilation currently is.
Definition compiler.h:231
const sourcemeta::core::JSON::String & keyword
The schema keyword.
Definition compiler.h:79
const std::vector< std::string > resources
The set of all schema resources in the schema without duplicates.
Definition compiler.h:208
auto push_back(InstructionExtra &&entry) -> void
Make room for another instruction, throwing once out of room.
Definition compiler.h:128
std::uint64_t max_depth
Definition compiler.h:194
auto mode() const -> Mode
The mode the whole schema is being compiled in.
Definition compiler.h:285
const sourcemeta::core::SchemaResolver & resolver
The schema resolver in use.
Definition compiler.h:212
const bool collects_annotations
Whether evaluating the schema may collect at least one annotation.
Definition compiler.h:220
const sourcemeta::core::URI & base
The schema base URI.
Definition compiler.h:67
const Tweaks tweaks
The set of tweaks for the compiler.
Definition compiler.h:224
auto base() const -> const sourcemeta::core::URI &
The base URI of the subschema that declares the keyword.
Definition compiler.h:280
std::optional< std::unordered_set< sourcemeta::core::JSON::StringView > > annotations
Definition compiler.h:184
bool is_property_name
Whether the current schema targets a property name.
Definition compiler.h:70
std::set< sourcemeta::core::WeakPointer > static_dependencies
The absolute pointers of the static keyword dependencies.
Definition compiler.h:41
const Compiler & compiler
The schema compiler in use.
Definition compiler.h:214
bool unresolved
Definition compiler.h:47
const sourcemeta::core::JSON::String EMPTY_KEYWORD
Definition compiler.h:242
auto default_schema_compiler(const KeywordContext &, const Instructions &) -> Instructions
std::map< sourcemeta::core::JSON::String, SchemaUnevaluatedEntry > SchemaUnevaluatedEntries
Definition compiler.h:52
auto to_json(const Template &schema_template) -> sourcemeta::core::JSON
std::function< Instructions(const KeywordContext &, const Instructions &)> Compiler
Definition compiler.h:105
Definition compiler.h:201
Definition compiler.h:247
std::vector< Instruction > Instructions
Definition evaluator_instruction.h:282
Mode
Definition compiler.h:111
@ FastValidation
Attempt to get to a boolean result as fast as possible.
Definition compiler.h:113
@ Exhaustive
Perform exhaustive evaluation, including annotations.
Definition compiler.h:115