polserver/pol-core/bscript/compiler/ast/FunctionCall.cpp
Kevin Eady e3512d001b Add scope handling for functions and variables (#690)
* Add Report.debug

* Implement function call scoping

* Implement variable scoping
- Rename `Identifier::scope` to `calling_scope`, and concatenate the identifier scope into `name` if necessary.
- Track current scope in SemanticAnalyzer, similar to UserFunctionVisitor.
- Use current scope in SemanticAnalyzer when visiting identifiers.

* Update grammar for `::identifier` global scoping

* Introduce ScopableName for function resolution

* Address review comments
- Check for display_debugs missing

* Add ScopableName for identifier resolution

* Some cleanup
- Remove `Identifier::calling_scope`, as it is tracked in the semantic
  analyzer
- Refactor `ScopeName::exists` to `global` for better clarification of
  its use
- Removed `X::maybe_scoped_string/name`

* Address self-review comments

* Rename `Function::module_name` to `scope`
2024-10-10 18:06:04 +02:00

75 lines
2.6 KiB
C++

#include "FunctionCall.h"
#include <map>
#include <utility>
#include "bscript/compiler/ast/Argument.h"
#include "bscript/compiler/ast/FunctionParameterDeclaration.h"
#include "bscript/compiler/ast/Identifier.h"
#include "bscript/compiler/ast/ModuleFunctionDeclaration.h"
#include "bscript/compiler/ast/NodeVisitor.h"
#include "bscript/compiler/file/SourceLocation.h"
#include "bscript/compiler/model/FunctionLink.h"
namespace Pol::Bscript::Compiler
{
FunctionCall::FunctionCall( const SourceLocation& source_location, std::string calling_scope,
ScopableName scoped_name,
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( std::make_unique<ScopableName>( std::move( scoped_name ) ) )
{
}
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 ) );
}
void FunctionCall::accept( NodeVisitor& visitor )
{
visitor.visit_function_call( *this );
}
void FunctionCall::describe_to( std::string& w ) const
{
fmt::format_to( std::back_inserter( w ), "function-call({})", scoped_name->string() );
}
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();
else
internal_error( "function has not been resolved" );
}
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 "";
}
} // namespace Pol::Bscript::Compiler