Make tree visitors more type-safe by preventing implicit casts
This commit is contained in:
parent
438620db2f
commit
b5f46e77b6
5 changed files with 50 additions and 16 deletions
|
|
@ -1,6 +1,8 @@
|
|||
cmake_minimum_required(VERSION 3.30)
|
||||
project(pslang CXX)
|
||||
|
||||
set(CMAKE_CXX_STANDARD 23)
|
||||
|
||||
add_subdirectory(libs/types)
|
||||
add_subdirectory(libs/ast)
|
||||
add_subdirectory(libs/parser)
|
||||
|
|
|
|||
|
|
@ -18,12 +18,16 @@ namespace pslang::ast
|
|||
return [this](auto const & type){ return derived().apply(type); };
|
||||
}
|
||||
|
||||
auto apply(literal const & expression)
|
||||
template <typename Expression>
|
||||
requires (std::is_same_v<Expression, literal>)
|
||||
auto apply(Expression const & expression)
|
||||
{
|
||||
return std::visit(make_visitor(), expression);
|
||||
}
|
||||
|
||||
auto apply(expression const & expression)
|
||||
template <typename Expression>
|
||||
requires (std::is_same_v<Expression, expression>)
|
||||
auto apply(Expression const & expression)
|
||||
{
|
||||
return std::visit(make_visitor(), expression);
|
||||
}
|
||||
|
|
@ -42,12 +46,16 @@ namespace pslang::ast
|
|||
return [this](auto & type){ return derived().apply(type); };
|
||||
}
|
||||
|
||||
auto apply(literal & expression)
|
||||
template <typename Expression>
|
||||
requires (std::is_same_v<Expression, literal>)
|
||||
auto apply(Expression & expression)
|
||||
{
|
||||
return std::visit(make_visitor(), expression);
|
||||
}
|
||||
|
||||
auto apply(expression & expression)
|
||||
template <typename Expression>
|
||||
requires (std::is_same_v<Expression, expression>)
|
||||
auto apply(Expression & expression)
|
||||
{
|
||||
return std::visit(make_visitor(), expression);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -18,12 +18,16 @@ namespace pslang::ast
|
|||
return [this](auto const & type){ return derived().apply(type); };
|
||||
}
|
||||
|
||||
auto apply(statement const & statement)
|
||||
template <typename Statement>
|
||||
requires (std::is_same_v<Statement, statement>)
|
||||
auto apply(Statement const & statement)
|
||||
{
|
||||
return std::visit(make_visitor(), statement);
|
||||
}
|
||||
|
||||
void apply(statement_list const & statement_list)
|
||||
template <typename Statement>
|
||||
requires (std::is_same_v<Statement, statement_list>)
|
||||
void apply(Statement const & statement_list)
|
||||
{
|
||||
for (auto const & statement : statement_list.statements)
|
||||
derived().apply(*statement);
|
||||
|
|
@ -43,12 +47,16 @@ namespace pslang::ast
|
|||
return [this](auto & type){ return derived().apply(type); };
|
||||
}
|
||||
|
||||
auto apply(statement & statement)
|
||||
template <typename Statement>
|
||||
requires (std::is_same_v<Statement, statement>)
|
||||
auto apply(Statement & statement)
|
||||
{
|
||||
return std::visit(make_visitor(), statement);
|
||||
}
|
||||
|
||||
void apply(statement_list & statement_list)
|
||||
template <typename Statement>
|
||||
requires (std::is_same_v<Statement, statement_list>)
|
||||
void apply(Statement & statement_list)
|
||||
{
|
||||
for (auto & statement : statement_list.statements)
|
||||
derived().apply(*statement);
|
||||
|
|
|
|||
|
|
@ -18,12 +18,16 @@ namespace pslang::ast
|
|||
return [this](auto const & type){ return derived().apply(type); };
|
||||
}
|
||||
|
||||
auto apply(types::primitive_type const & type)
|
||||
template <typename Type>
|
||||
requires (std::is_same_v<Type, types::primitive_type>)
|
||||
auto apply(Type const & type)
|
||||
{
|
||||
return std::visit(make_visitor(), type);
|
||||
}
|
||||
|
||||
auto apply(ast::type const & type)
|
||||
template <typename Type>
|
||||
requires (std::is_same_v<Type, type>)
|
||||
auto apply(Type const & type)
|
||||
{
|
||||
return std::visit(make_visitor(), type);
|
||||
}
|
||||
|
|
@ -42,12 +46,16 @@ namespace pslang::ast
|
|||
return [this](auto & type){ return derived().apply(type); };
|
||||
}
|
||||
|
||||
auto apply(types::primitive_type & type)
|
||||
template <typename Type>
|
||||
requires (std::is_same_v<Type, types::primitive_type>)
|
||||
auto apply(Type & type)
|
||||
{
|
||||
return std::visit(make_visitor(), type);
|
||||
}
|
||||
|
||||
auto apply(ast::type & type)
|
||||
template <typename Type>
|
||||
requires (std::is_same_v<Type, type>)
|
||||
auto apply(Type & type)
|
||||
{
|
||||
return std::visit(make_visitor(), type);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -18,12 +18,16 @@ namespace pslang::types
|
|||
return [this](auto const & type){ return derived().apply(type); };
|
||||
}
|
||||
|
||||
auto apply(types::primitive_type const & type)
|
||||
template <typename Type>
|
||||
requires (std::is_same_v<Type, primitive_type>)
|
||||
auto apply(Type const & type)
|
||||
{
|
||||
return std::visit(make_visitor(), type);
|
||||
}
|
||||
|
||||
auto apply(types::type const & type)
|
||||
template <typename Type>
|
||||
requires (std::is_same_v<Type, type>)
|
||||
auto apply(Type const & type)
|
||||
{
|
||||
return std::visit(make_visitor(), type);
|
||||
}
|
||||
|
|
@ -42,12 +46,16 @@ namespace pslang::types
|
|||
return [this](auto & type){ return derived().apply(type); };
|
||||
}
|
||||
|
||||
auto apply(types::primitive_type & type)
|
||||
template <typename Type>
|
||||
requires (std::is_same_v<Type, primitive_type>)
|
||||
auto apply(Type & type)
|
||||
{
|
||||
return std::visit(make_visitor(), type);
|
||||
}
|
||||
|
||||
auto apply(types::type & type)
|
||||
template <typename Type>
|
||||
requires (std::is_same_v<Type, type>)
|
||||
auto apply(Type & type)
|
||||
{
|
||||
return std::visit(make_visitor(), type);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue