2020-08-23 15:01:39 -07:00
|
|
|
#include "Function.h"
|
|
|
|
|
|
2021-02-25 16:53:22 +01:00
|
|
|
#include "bscript/compiler/ast/FunctionBody.h"
|
|
|
|
|
#include "bscript/compiler/ast/FunctionParameterDeclaration.h"
|
|
|
|
|
#include "bscript/compiler/ast/FunctionParameterList.h"
|
2020-08-23 15:01:39 -07:00
|
|
|
|
|
|
|
|
namespace Pol::Bscript::Compiler
|
|
|
|
|
{
|
2024-08-11 23:01:38 +02:00
|
|
|
Function::Function( const SourceLocation& source_location, std::string scope, std::string name,
|
2020-08-31 18:46:27 -07:00
|
|
|
std::unique_ptr<FunctionParameterList> parameter_list,
|
|
|
|
|
std::unique_ptr<FunctionBody> body )
|
2024-08-11 23:01:38 +02:00
|
|
|
: Node( source_location ), scope( std::move( scope ) ), name( std::move( name ) )
|
2020-08-31 18:46:27 -07:00
|
|
|
{
|
|
|
|
|
children.reserve( 2 );
|
|
|
|
|
children.push_back( std::move( parameter_list ) );
|
|
|
|
|
children.push_back( std::move( body ) );
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-11 23:01:38 +02:00
|
|
|
Function::Function( const SourceLocation& source_location, std::string scope, std::string name,
|
2020-08-23 15:01:39 -07:00
|
|
|
std::unique_ptr<FunctionParameterList> parameter_list )
|
2024-08-11 23:01:38 +02:00
|
|
|
: Node( source_location, std::move( parameter_list ) ),
|
|
|
|
|
scope( std::move( scope ) ),
|
|
|
|
|
name( std::move( name ) )
|
2020-08-23 15:01:39 -07:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
unsigned Function::parameter_count() const
|
|
|
|
|
{
|
2024-01-12 06:51:44 +01:00
|
|
|
return static_cast<unsigned>( children.at( 0 )->children.size() );
|
2020-08-23 15:01:39 -07:00
|
|
|
}
|
|
|
|
|
|
2024-08-04 17:13:30 +02:00
|
|
|
bool Function::is_variadic() const
|
|
|
|
|
{
|
|
|
|
|
// The variadic argument, if present, is the last one
|
|
|
|
|
if ( parameter_count() > 0 )
|
|
|
|
|
{
|
|
|
|
|
const auto& parameter_list = children.at( 0 );
|
|
|
|
|
|
|
|
|
|
auto parameter =
|
|
|
|
|
static_cast<FunctionParameterDeclaration*>( parameter_list->children.back().get() );
|
|
|
|
|
|
|
|
|
|
return parameter->rest;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-11 23:01:38 +02:00
|
|
|
std::string Function::scoped_name() const
|
|
|
|
|
{
|
|
|
|
|
if ( scope.empty() )
|
|
|
|
|
{
|
|
|
|
|
return name;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return fmt::format( "{}::{}", scope, name );
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-23 15:01:39 -07:00
|
|
|
std::vector<std::reference_wrapper<FunctionParameterDeclaration>> Function::parameters()
|
|
|
|
|
{
|
|
|
|
|
std::vector<std::reference_wrapper<FunctionParameterDeclaration>> params;
|
|
|
|
|
auto& param_list = child<FunctionParameterList>( 0 );
|
|
|
|
|
for ( auto& param : param_list.children )
|
|
|
|
|
{
|
|
|
|
|
params.emplace_back( *static_cast<FunctionParameterDeclaration*>( param.get() ) );
|
|
|
|
|
}
|
|
|
|
|
return params;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace Pol::Bscript::Compiler
|