Rewrite get_location(statement/expression) using visitors

This commit is contained in:
Nikita Lisitsa 2025-12-22 18:52:57 +03:00
parent b5f46e77b6
commit 35435ac626
2 changed files with 113 additions and 31 deletions

View file

@ -1,4 +1,5 @@
#include <pslang/ast/expression.hpp> #include <pslang/ast/expression.hpp>
#include <pslang/ast/expression_visitor.hpp>
namespace pslang::ast namespace pslang::ast
{ {
@ -6,33 +7,63 @@ namespace pslang::ast
namespace namespace
{ {
struct get_location_visitor
: const_expression_visitor<get_location_visitor>
{
using const_expression_visitor::apply;
template <typename T> template <typename T>
location get_location_impl(numeric_literal_base<T> const & expression) location apply(numeric_literal_base<T> const & node)
{ {
return expression.location; return node.location;
} }
location get_location_impl(literal const & expression) location apply(identifier const & node)
{ {
return std::visit([](auto const & expression){ return get_location_impl(expression); }, expression); return node.location;
} }
template <typename Expression> location apply(unary_operation const & node)
location get_location_impl(Expression const & expression)
{ {
return expression.location; return node.location;
} }
location get_location_impl(expression const & expression) location apply(binary_operation const & node)
{ {
return std::visit([](auto const & expression){ return get_location_impl(expression); }, expression); return node.location;
} }
location apply(cast_operation const & node)
{
return node.location;
}
location apply(function_call const & node)
{
return node.location;
}
location apply(array const & node)
{
return node.location;
}
location apply(array_access const & node)
{
return node.location;
}
location apply(field_access const & node)
{
return node.location;
}
};
} }
location get_location(expression const & expression) location get_location(expression const & expression)
{ {
return get_location_impl(expression); return get_location_visitor{}.apply(expression);
} }
} }

View file

@ -1,4 +1,5 @@
#include <pslang/ast/statement.hpp> #include <pslang/ast/statement.hpp>
#include <pslang/ast/statement_visitor.hpp>
namespace pslang::ast namespace pslang::ast
{ {
@ -6,27 +7,77 @@ namespace pslang::ast
namespace namespace
{ {
location get_location_impl(expression_ptr const & statement) struct get_location_visitor
: const_statement_visitor<get_location_visitor>
{ {
return get_location(*statement); using const_statement_visitor::apply;
location apply(expression_ptr const & node)
{
return get_location(*node);
} }
template <typename Statement> location apply(assignment const & node)
location get_location_impl(Statement const & statement)
{ {
return statement.location; return node.location;
} }
location get_location_impl(statement const & statement) location apply(variable_declaration const & node)
{ {
return std::visit([](auto const & statement){ return get_location_impl(statement); }, statement); return node.location;
} }
location apply(if_block const & node)
{
return node.location;
}
location apply(else_block const & node)
{
return node.location;
}
location apply(else_if_block const & node)
{
return node.location;
}
location apply(if_chain const & node)
{
return node.location;
}
location apply(while_block const & node)
{
return node.location;
}
location apply(function_definition const & node)
{
return node.location;
}
location apply(return_statement const & node)
{
return node.location;
}
location apply(field_definition const & node)
{
return node.location;
}
location apply(struct_definition const & node)
{
return node.location;
}
};
} }
location get_location(statement const & statement) location get_location(statement const & statement)
{ {
return get_location_impl(statement); return get_location_visitor{}.apply(statement);
} }
} }