#include "parse.h"
using namespace goose;
using namespace goose::parse;
using namespace goose::builtins;
VisibilityScope::VisibilityScope( Parser& p ) :
m_identityGuard( p )
{
auto parentBaseIdentity = p.context().identity();
parentBaseIdentity = TakeVectorTerm( parentBaseIdentity, VecSize( parentBaseIdentity ) - 1 );
// We construct an identity to hold all the compilation time constants for this scope,
// that will be visible in every nested scopes and functions.
// We create a separate identity for the local variables, from which both all inherited constants
// and local variables from the parent scope will be visible. That one will be made current,
// and can be truncated to find the "compmilation constant" identity where we need to see only the
// compilation constants (for instance, in parameter lists).
// TODO: see if we can replace this unique id with something that is just unique within the current
// function. This would avoid wild changes in the mangled names based on unrelated definitions being added
// elsewhere.
auto localIdentity = AppendToVectorTerm( parentBaseIdentity, TERM( StringId( Env::NewUniqueId() ) ) );
auto localVarsIdentity = AppendToVectorTerm( localIdentity, TSID( locvars ) );
p.context().env()->addVisibilityRule( parentBaseIdentity, localIdentity );
p.context().env()->addVisibilityRule( p.context().identity(), localVarsIdentity );
p.context().setIdentity( localVarsIdentity );
}