Goose  Artifact [0540fa184a]

Artifact 0540fa184aaa1743a8b02c7c8e19e18c06bfb61364a57f58fa278e2bb69891a8:

  • File bs/sema/uni-context.cpp — part of check-in [d126da38f7] at 2018-12-26 00:48:18 on branch trunk — Implement hole half-unification. (user: achavasse size: 1857)

#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 );
}