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 {
46 const sourcemeta::core::JSON &
schema;
50 const sourcemeta::core::URI &
base;
62 const sourcemeta::core::JSON::String &
keyword;
73 [[nodiscard]]
auto value() const -> const sourcemeta::core::JSON & {
74 assert(this->keyword_value !=
nullptr);
75 return *this->keyword_value;
94enum class Mode : std::uint8_t {
106class SOURCEMETA_BLAZE_COMPILER_EXPORT InstructionExtras {
108 explicit InstructionExtras(
const std::uint64_t limit) : limit_{limit} {}
112 if (this->entries_.size() >= this->limit_) [[unlikely]] {
116 this->entries_.push_back(std::move(entry));
119 [[nodiscard]]
auto size() const noexcept -> std::
size_t {
120 return this->entries_.size();
123 [[nodiscard]]
auto operator[](
const std::size_t index)
const noexcept
124 ->
const InstructionExtra & {
125 return this->entries_[index];
128 [[nodiscard]]
auto operator[](
const std::size_t index)
noexcept
129 -> InstructionExtra & {
130 return this->entries_[index];
134 [[nodiscard]]
auto release() && -> std::vector<InstructionExtra> {
135 return std::move(this->entries_);
143#pragma warning(disable : 4251 4275)
145 std::vector<InstructionExtra> entries_;
147#pragma warning(default : 4251 4275)
149 std::uint64_t limit_;
166 std::optional<std::unordered_set<sourcemeta::core::JSON::StringView>>
177 std::uint64_t
max_depth{std::numeric_limits<std::uint64_t>::max()};
199 const sourcemeta::core::JSON &
root;
201 const sourcemeta::core::SchemaFrame &
frame;
205 const sourcemeta::core::SchemaWalker &
walker;
224 const std::map<sourcemeta::core::JSON::String, UnevaluatedEntry>
unevaluated;
235 std::tuple<sourcemeta::core::SchemaReferenceType, std::string_view, bool>,
236 std::pair<std::size_t, const sourcemeta::core::WeakPointer *>>
267 [[nodiscard]]
auto keyword() const -> const sourcemeta::core::JSON::String & {
272 [[nodiscard]]
auto value() const -> const sourcemeta::core::JSON & {
273 return this->target.
value();
277 [[nodiscard]]
auto schema() const -> const sourcemeta::core::JSON & {
278 return this->local.
schema;
283 -> const sourcemeta::core::SchemaVocabularies & {
288 [[nodiscard]]
auto base() const -> const sourcemeta::core::URI & {
289 return this->local.
base;
299 .global = this->global,
300 .local = this->local,
302 .base_schema_location = sourcemeta::core::EMPTY_WEAK_POINTER,
303 .base_instance_location =
304 sourcemeta::core::EMPTY_WEAK_POINTER}};
344auto SOURCEMETA_BLAZE_COMPILER_EXPORT
345compile(
const sourcemeta::core::JSON &schema,
346 const sourcemeta::core::SchemaWalker &walker,
347 const sourcemeta::core::SchemaResolver &resolver,
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())
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,
371 const std::optional<Tweaks> &tweaks = std::nullopt) ->
Template;
381auto SOURCEMETA_BLAZE_COMPILER_EXPORT
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;
391 -> sourcemeta::core::JSON;
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
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:255
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
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