2024-03-08 01:24:13 -03:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2023-2024, Dan Klishch <danilklishch@gmail.com>
|
|
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "Parser/Lexer.h"
|
2024-03-08 01:33:36 -03:00
|
|
|
#include "Parser/SpecificationParsing.h"
|
2024-03-08 01:24:13 -03:00
|
|
|
#include "Parser/XMLUtils.h"
|
|
|
|
|
|
|
|
|
|
namespace JSSpecCompiler {
|
|
|
|
|
|
2024-03-08 01:29:43 -03:00
|
|
|
bool SpecificationFunction::post_initialize(XML::Node const* element)
|
2024-03-08 01:24:13 -03:00
|
|
|
{
|
|
|
|
|
VERIFY(element->as_element().name == tag_emu_clause);
|
|
|
|
|
|
|
|
|
|
auto& ctx = context();
|
2024-03-11 16:51:14 -03:00
|
|
|
m_location = ctx.location_from_xml_offset(element->offset);
|
2024-03-08 01:24:13 -03:00
|
|
|
|
|
|
|
|
auto maybe_id = get_attribute_by_name(element, attribute_id);
|
|
|
|
|
if (!maybe_id.has_value()) {
|
2024-03-11 16:51:14 -03:00
|
|
|
ctx.diag().error(m_location,
|
2024-03-08 01:24:13 -03:00
|
|
|
"no id attribute");
|
|
|
|
|
} else {
|
|
|
|
|
m_id = maybe_id.value();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_header.header.visit(
|
2024-03-11 16:51:14 -03:00
|
|
|
[&](AbstractOperationDeclaration const& abstract_operation) {
|
|
|
|
|
m_declaration = abstract_operation;
|
2024-03-08 01:24:13 -03:00
|
|
|
|
2024-03-11 16:51:14 -03:00
|
|
|
auto abstract_operation_id = get_attribute_by_name(element, attribute_aoid).value();
|
2024-03-08 01:24:13 -03:00
|
|
|
|
2024-03-11 16:51:14 -03:00
|
|
|
if (abstract_operation.name != abstract_operation_id) {
|
|
|
|
|
ctx.diag().warn(m_location,
|
2024-03-08 01:24:13 -03:00
|
|
|
"function name in header and <emu-clause>[aoid] do not match");
|
|
|
|
|
}
|
|
|
|
|
},
|
2024-03-11 16:51:14 -03:00
|
|
|
[&](OneOf<AccessorDeclaration, MethodDeclaration> auto const& declaration) {
|
|
|
|
|
m_declaration = declaration;
|
2024-03-08 01:24:13 -03:00
|
|
|
},
|
|
|
|
|
[&](auto const&) {
|
|
|
|
|
VERIFY_NOT_REACHED();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
Vector<XML::Node const*> algorithm_nodes;
|
|
|
|
|
|
|
|
|
|
for (auto const& child : element->as_element().children) {
|
|
|
|
|
child->content.visit(
|
|
|
|
|
[&](XML::Node::Element const& element) {
|
|
|
|
|
if (element.name == tag_h1) {
|
|
|
|
|
// Processed in SpecificationClause
|
|
|
|
|
} else if (element.name == tag_p) {
|
|
|
|
|
ctx.diag().warn(ctx.location_from_xml_offset(child->offset),
|
|
|
|
|
"prose is ignored");
|
|
|
|
|
} else if (element.name == tag_emu_alg) {
|
|
|
|
|
algorithm_nodes.append(child);
|
|
|
|
|
} else {
|
|
|
|
|
ctx.diag().error(ctx.location_from_xml_offset(child->offset),
|
|
|
|
|
"<{}> should not be a child of <emu-clause> specifing function"sv, element.name);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
[&](auto const&) {});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (algorithm_nodes.size() != 1) {
|
2024-03-11 16:51:14 -03:00
|
|
|
ctx.diag().error(m_location,
|
2024-03-08 01:24:13 -03:00
|
|
|
"<emu-clause> specifing function should have exactly one <emu-alg> child"sv);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto maybe_algorithm = Algorithm::create(ctx, algorithm_nodes[0]);
|
|
|
|
|
if (maybe_algorithm.has_value()) {
|
|
|
|
|
m_algorithm = maybe_algorithm.release_value();
|
|
|
|
|
return true;
|
|
|
|
|
} else {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-08 01:29:43 -03:00
|
|
|
void SpecificationFunction::do_collect(TranslationUnitRef translation_unit)
|
2024-03-08 01:24:13 -03:00
|
|
|
{
|
2024-03-11 16:51:14 -03:00
|
|
|
translation_unit->adopt_function(make_ref_counted<FunctionDefinition>(m_declaration.release_value(), m_location, m_algorithm.tree()));
|
2024-03-08 01:24:13 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|