#include "sema.h"
using namespace empathy;
using namespace empathy::sema;
UnificationContext::UnificationContext( const ptr< UnificationRuleSet >& rules ) :
m_rules( rules )
{}
UnificationContext::UnificationContext( ptr< UnificationRuleSet >&& rules ) :
m_rules( move( rules ) )
{}
optional< uint32_t > UnificationContext::getLHSHoleIndex( const StringId& name ) const
{
HoleName holeName( name, m_currentLHSNamespaceIndex );
auto it = m_pCoW->m_holes.find( holeName );
if( it == m_pCoW->m_holes.end() )
return nullopt;
return it->second;
}
optional< uint32_t > UnificationContext::getRHSHoleIndex( const StringId& name ) const
{
HoleName holeName( name, m_currentRHSNamespaceIndex );
auto it = m_pCoW->m_holes.find( holeName );
if( it == m_pCoW->m_holes.end() )
return nullopt;
return it->second;
}
uint32_t UnificationContext::createValue()
{
m_values = m_values.push_back( nullopt );
uint32_t index = m_values.size() - 1;
// Add the new value as a dependency of the current one, if any.
addDependency( index );
return index;
}
void UnificationContext::setLHSHoleIndex( const StringId& name, uint32_t index )
{
HoleName holeName( name, m_currentLHSNamespaceIndex );
CoW( m_pCoW )->m_holes.emplace( holeName, index );
}
void UnificationContext::setRHSHoleIndex( const StringId& name, uint32_t index )
{
HoleName holeName( name, m_currentRHSNamespaceIndex );
CoW( m_pCoW )->m_holes.emplace( holeName, index );
}
void UnificationContext::addDependency( size_t holeIndex )
{
if( m_currentHoleIndex == InvalidHoleIndex )
return;
addDependency( holeIndex, m_currentHoleIndex );
}
void UnificationContext::addDependency( size_t srcHoleIndex, size_t dstHoleIndex )
{
CoW( m_pCoW )->m_depGraph.addDep( srcHoleIndex, dstHoleIndex );
}