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_visitor.hpp>
namespace pslang::ast
{
@ -6,33 +7,63 @@ namespace pslang::ast
namespace
{
template <typename T>
location get_location_impl(numeric_literal_base<T> const & expression)
struct get_location_visitor
: const_expression_visitor<get_location_visitor>
{
return expression.location;
}
using const_expression_visitor::apply;
location get_location_impl(literal const & expression)
{
return std::visit([](auto const & expression){ return get_location_impl(expression); }, expression);
}
template <typename T>
location apply(numeric_literal_base<T> const & node)
{
return node.location;
}
template <typename Expression>
location get_location_impl(Expression const & expression)
{
return expression.location;
}
location apply(identifier const & node)
{
return node.location;
}
location get_location_impl(expression const & expression)
{
return std::visit([](auto const & expression){ return get_location_impl(expression); }, expression);
}
location apply(unary_operation const & node)
{
return node.location;
}
location apply(binary_operation const & node)
{
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)
{
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_visitor.hpp>
namespace pslang::ast
{
@ -6,27 +7,77 @@ namespace pslang::ast
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;
template <typename Statement>
location get_location_impl(Statement const & statement)
{
return statement.location;
}
location apply(expression_ptr const & node)
{
return get_location(*node);
}
location get_location_impl(statement const & statement)
{
return std::visit([](auto const & statement){ return get_location_impl(statement); }, statement);
}
location apply(assignment const & node)
{
return node.location;
}
location apply(variable_declaration const & node)
{
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)
{
return get_location_impl(statement);
return get_location_visitor{}.apply(statement);
}
}