2020-08-24 01:47:38 -07:00
|
|
|
#include "FunctionCall.h"
|
|
|
|
|
|
2024-01-16 17:55:42 +01:00
|
|
|
|
2020-08-24 01:47:38 -07:00
|
|
|
#include <map>
|
|
|
|
|
#include <utility>
|
|
|
|
|
|
2021-02-25 16:53:22 +01:00
|
|
|
#include "bscript/compiler/ast/Argument.h"
|
|
|
|
|
#include "bscript/compiler/ast/FunctionParameterDeclaration.h"
|
2024-07-28 20:21:23 +02:00
|
|
|
#include "bscript/compiler/ast/Identifier.h"
|
2021-02-25 16:53:22 +01:00
|
|
|
#include "bscript/compiler/ast/ModuleFunctionDeclaration.h"
|
|
|
|
|
#include "bscript/compiler/ast/NodeVisitor.h"
|
|
|
|
|
#include "bscript/compiler/file/SourceLocation.h"
|
|
|
|
|
#include "bscript/compiler/model/FunctionLink.h"
|
2020-08-24 01:47:38 -07:00
|
|
|
|
|
|
|
|
namespace Pol::Bscript::Compiler
|
|
|
|
|
{
|
2024-08-11 23:01:38 +02:00
|
|
|
FunctionCall::FunctionCall( const SourceLocation& source_location, std::string calling_scope,
|
|
|
|
|
ScopableName scoped_name,
|
2024-07-28 20:21:23 +02:00
|
|
|
std::vector<std::unique_ptr<Argument>> arguments )
|
|
|
|
|
: Expression( source_location, std::move( arguments ) ),
|
2024-08-11 23:01:38 +02:00
|
|
|
function_link( std::make_shared<FunctionLink>( source_location, calling_scope ) ),
|
|
|
|
|
calling_scope( std::move( calling_scope ) ),
|
|
|
|
|
scoped_name( std::make_unique<ScopableName>( std::move( scoped_name ) ) )
|
2020-08-24 01:47:38 -07:00
|
|
|
{
|
2024-08-11 23:01:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FunctionCall::FunctionCall( const SourceLocation& source_location, std::string calling_scope,
|
|
|
|
|
std::unique_ptr<Node> callee,
|
|
|
|
|
std::vector<std::unique_ptr<Argument>> arguments )
|
|
|
|
|
: Expression( source_location, std::move( arguments ) ),
|
|
|
|
|
function_link( std::make_shared<FunctionLink>( source_location, calling_scope ) ),
|
|
|
|
|
calling_scope( std::move( calling_scope ) ),
|
|
|
|
|
scoped_name( nullptr ) // FunctionCalls with expression-as-callees do not have a scoped name
|
|
|
|
|
{
|
|
|
|
|
children.insert( children.begin(), std::move( callee ) );
|
2020-08-24 01:47:38 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FunctionCall::accept( NodeVisitor& visitor )
|
|
|
|
|
{
|
|
|
|
|
visitor.visit_function_call( *this );
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-12 06:51:44 +01:00
|
|
|
void FunctionCall::describe_to( std::string& w ) const
|
2020-08-24 01:47:38 -07:00
|
|
|
{
|
2024-08-28 20:48:07 +02:00
|
|
|
if ( scoped_name )
|
|
|
|
|
{
|
|
|
|
|
fmt::format_to( std::back_inserter( w ), "function-call({})", scoped_name->string() );
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
fmt::format_to( std::back_inserter( w ), "expression-as-callee function-call" );
|
|
|
|
|
}
|
2020-08-24 01:47:38 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::vector<std::unique_ptr<Argument>> FunctionCall::take_arguments()
|
|
|
|
|
{
|
|
|
|
|
std::vector<std::unique_ptr<Argument>> args;
|
|
|
|
|
args.reserve( children.size() );
|
|
|
|
|
for ( auto& child : children )
|
|
|
|
|
{
|
|
|
|
|
args.emplace_back( static_unique_pointer_cast<Argument, Node>( std::move( child ) ) );
|
|
|
|
|
}
|
|
|
|
|
return args;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::vector<std::reference_wrapper<FunctionParameterDeclaration>> FunctionCall::parameters() const
|
|
|
|
|
{
|
|
|
|
|
if ( auto fn = function_link->function() )
|
|
|
|
|
return fn->parameters();
|
2026-01-18 09:35:52 +01:00
|
|
|
internal_error( "function has not been resolved" );
|
2020-08-24 01:47:38 -07:00
|
|
|
}
|
|
|
|
|
|
2024-08-11 23:01:38 +02:00
|
|
|
std::string FunctionCall::string() const
|
|
|
|
|
{
|
|
|
|
|
// Should only be used on compile-time function calls (ie. not an
|
|
|
|
|
// expression-as-callee function call) that actually have a ScopedName.
|
|
|
|
|
if ( scoped_name )
|
|
|
|
|
return scoped_name->string();
|
|
|
|
|
return "";
|
|
|
|
|
}
|
2020-08-24 01:47:38 -07:00
|
|
|
} // namespace Pol::Bscript::Compiler
|