2020-08-30 01:32:38 -07:00
|
|
|
#include "LocalVariableScope.h"
|
|
|
|
|
|
2021-02-25 16:53:22 +01:00
|
|
|
#include "bscript/compiler/Report.h"
|
|
|
|
|
#include "bscript/compiler/analyzer/LocalVariableScopes.h"
|
|
|
|
|
#include "bscript/compiler/analyzer/Variables.h"
|
|
|
|
|
#include "bscript/compiler/file/SourceLocation.h"
|
|
|
|
|
#include "bscript/compiler/model/LocalVariableScopeInfo.h"
|
|
|
|
|
#include "bscript/compiler/model/Variable.h"
|
2020-08-30 01:32:38 -07:00
|
|
|
|
|
|
|
|
namespace Pol::Bscript::Compiler
|
|
|
|
|
{
|
|
|
|
|
LocalVariableScope::LocalVariableScope( LocalVariableScopes& scopes,
|
2020-09-12 09:31:52 -07:00
|
|
|
LocalVariableScopeInfo& local_variable_scope_info )
|
2020-08-30 01:32:38 -07:00
|
|
|
: scopes( scopes ),
|
|
|
|
|
report( scopes.report ),
|
|
|
|
|
block_depth( scopes.local_variable_scopes.size() ),
|
|
|
|
|
prev_locals( scopes.local_variables.count() ),
|
2020-09-12 09:31:52 -07:00
|
|
|
local_variable_scope_info( local_variable_scope_info )
|
2020-08-30 01:32:38 -07:00
|
|
|
{
|
2020-09-12 09:31:52 -07:00
|
|
|
local_variable_scope_info.base_index = prev_locals;
|
2020-08-30 01:32:38 -07:00
|
|
|
scopes.local_variable_scopes.push_back( this );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LocalVariableScope::~LocalVariableScope()
|
|
|
|
|
{
|
|
|
|
|
scopes.local_variables.remove_all_but( prev_locals );
|
|
|
|
|
|
|
|
|
|
for ( auto& variable : shadowing )
|
|
|
|
|
{
|
|
|
|
|
scopes.local_variables.restore_shadowed( variable );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
scopes.local_variable_scopes.pop_back();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::shared_ptr<Variable> LocalVariableScope::create( const std::string& name, WarnOn warn_on,
|
|
|
|
|
const SourceLocation& source_location )
|
|
|
|
|
{
|
|
|
|
|
if ( auto existing = scopes.local_variables.find( name ) )
|
|
|
|
|
{
|
|
|
|
|
if ( existing->block_depth == block_depth )
|
|
|
|
|
{
|
|
|
|
|
report.error( source_location, "Variable '", name, "' is already in scope.\n",
|
|
|
|
|
" See previous definition at: ",
|
|
|
|
|
existing->source_location, "\n" );
|
|
|
|
|
return existing;
|
|
|
|
|
}
|
|
|
|
|
shadowing.push_back( existing );
|
|
|
|
|
}
|
|
|
|
|
auto local = scopes.local_variables.create( name, block_depth, warn_on,
|
|
|
|
|
source_location );
|
|
|
|
|
|
2020-09-12 09:31:52 -07:00
|
|
|
local_variable_scope_info.variables.push_back( local );
|
2020-08-30 01:32:38 -07:00
|
|
|
|
|
|
|
|
return local;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace Pol::Bscript::Compiler
|