/*
 * This file is part of the Cforall project
 *
 * $Id: TypeEnvironment.cc,v 1.7 2005/08/29 20:14:16 rcbilson Exp $
 *
 */

#include <algorithm>
#include <iterator>

#include "TypeEnvironment.h"
#include "SynTree/Type.h"
#include "SynTree/TypeSubstitution.h"
#include "utility.h"

namespace ResolvExpr {

void printAssertionSet( const AssertionSet &assertions, std::ostream &os, int indent )
{
  for( AssertionSet::const_iterator i = assertions.begin(); i != assertions.end(); ++i ) {
    i->first->print( os, indent );
    if( i->second ) {
      os << "(used)";
    } else {
      os << "(not used)";
    }
  }
}

void printOpenVarSet( const OpenVarSet &openVars, std::ostream &os, int indent )
{
  os << std::string( indent, ' ' );
  for( OpenVarSet::const_iterator i = openVars.begin(); i != openVars.end(); ++i ) {
    os << i->first << "(" << i->second << ") ";
  }
}

void
EqvClass::initialize( const EqvClass &src, EqvClass &dest )
{
  dest.vars = src.vars;
  dest.type = maybeClone( src.type );
  dest.allowWidening = src.allowWidening;
  dest.kind = src.kind;
}

EqvClass::EqvClass() : type( 0 ), allowWidening( true )
{
}

EqvClass::EqvClass( const EqvClass &other )
{
  initialize( other, *this );
}

EqvClass &
EqvClass::operator=( const EqvClass &other )
{
  if( this == &other ) return *this;
  delete type;
  initialize( other, *this );
  return *this;
}

EqvClass::~EqvClass()
{
  delete type;
}

void 
EqvClass::print( std::ostream &os, int indent ) const
{
  os << std::string( indent, ' ' ) << "( ";
  std::copy( vars.begin(), vars.end(), std::ostream_iterator< std::string >( os, " " ) );
  os << ")";
  if( type ) {
    os << " -> ";
    type->print( os, indent );
  }
  if( !allowWidening ) {
    os << " (no widening)";
  }
  os << std::endl;
}

bool 
TypeEnvironment::lookup( const std::string &var, EqvClass &eqvClass ) const
{
  for( std::list< EqvClass >::const_iterator i = env.begin(); i != env.end(); ++i ) {
    if( i->vars.find( var ) != i->vars.end() ) {
///       std::cout << var << " is in class ";
///       i->print( std::cout );
      eqvClass = *i;
      return true;
    }
///     std::cout << var << " is not in class ";
///     i->print( std::cout );
  }
  return false;
}

void 
TypeEnvironment::add( const EqvClass &eqvClass )
{
  std::list< EqvClass >::iterator i = env.begin();
  while( i != env.end() ) {
    std::list< EqvClass >::iterator next = i;
    next++;
    std::set< std::string > intersection;
    std::set_intersection( i->vars.begin(), i->vars.end(), eqvClass.vars.begin(), eqvClass.vars.end(), std::inserter( intersection, intersection.begin() ) );
    if( !intersection.empty() ) {
      env.erase( i );
    }
    i = next;
  }
  env.insert( env.end(), eqvClass );
}

void 
TypeEnvironment::add( const std::list< TypeDecl* > &tyDecls )
{
  for( std::list< TypeDecl* >::const_iterator i = tyDecls.begin(); i != tyDecls.end(); ++i ) {
    EqvClass newClass;
    newClass.vars.insert( (*i)->get_name() );
    newClass.kind = (*i)->get_kind();
    env.push_back( newClass );
  }
}

void 
TypeEnvironment::makeSubstitution( TypeSubstitution &sub ) const
{
  for( std::list< EqvClass >::const_iterator theClass = env.begin(); theClass != env.end(); ++theClass ) {
    for( std::set< std::string >::const_iterator theVar = theClass->vars.begin(); theVar != theClass->vars.end(); ++theVar ) {
///       std::cout << "adding " << *theVar;
      if( theClass->type ) {
///         std::cout << " bound to ";
///         theClass->type->print( std::cout );
///         std::cout << std::endl;
        sub.add( *theVar, theClass->type );
      } else if( theVar != theClass->vars.begin() ) {
        TypeInstType *newTypeInst = new TypeInstType( Type::Qualifiers(), *theClass->vars.begin(), theClass->kind == TypeDecl::Ftype );
///         std::cout << " bound to variable " << *theClass->vars.begin() << std::endl;
        sub.add( *theVar, newTypeInst );
        delete newTypeInst;
      }
    }
  }
///   std::cerr << "input env is:" << std::endl;
///   print( std::cerr, 8 );
///   std::cerr << "sub is:" << std::endl;
///   sub.print( std::cerr, 8 );
  sub.normalize();
}

void 
TypeEnvironment::print( std::ostream &os, int indent ) const
{
  for( std::list< EqvClass >::const_iterator i = env.begin(); i != env.end(); ++i ) {
    i->print( os, indent );
  }
}

std::list< EqvClass >::iterator 
TypeEnvironment::internal_lookup( const std::string &var )
{
  for( std::list< EqvClass >::iterator i = env.begin(); i != env.end(); ++i ) {
    if( i->vars.find( var ) == i->vars.end() ) {
      return i;
    }
  }
  return env.end();
}

void 
TypeEnvironment::simpleCombine( const TypeEnvironment &second )
{
  env.insert( env.end(), second.env.begin(), second.env.end() );
}

void 
TypeEnvironment::combine( const TypeEnvironment &second, Type *(*combineFunc)( Type*, Type* ) )
{
  TypeEnvironment secondCopy( second );
  for( std::list< EqvClass >::iterator firstClass = env.begin(); firstClass != env.end(); ++firstClass ) {
    EqvClass &newClass = *firstClass;
    std::set< std::string > newVars;
    for( std::set< std::string >::const_iterator var = firstClass->vars.begin(); var != firstClass->vars.end(); ++var ) {
      std::list< EqvClass >::iterator secondClass = secondCopy.internal_lookup( *var );
      if( secondClass != secondCopy.env.end() ) {
        newVars.insert( secondClass->vars.begin(), secondClass->vars.end() );
        if( secondClass->type ) {
          if( newClass.type ) {
            Type *newType = combineFunc( newClass.type, secondClass->type );
            delete newClass.type;
            newClass.type = newType;
            newClass.allowWidening = newClass.allowWidening && secondClass->allowWidening;
          } else {
            newClass.type = secondClass->type->clone();
            newClass.allowWidening = secondClass->allowWidening;
          }
        }
        secondCopy.env.erase( secondClass );
      }
    }
    newClass.vars.insert( newVars.begin(), newVars.end() );
  }
  for( std::list< EqvClass >::iterator secondClass = secondCopy.env.begin(); secondClass != secondCopy.env.end(); ++secondClass ) {
    env.push_back( *secondClass );
  }
}

void 
TypeEnvironment::extractOpenVars( OpenVarSet &openVars ) const
{
  for( std::list< EqvClass >::const_iterator eqvClass = env.begin(); eqvClass != env.end(); ++eqvClass ) {
    for( std::set< std::string >::const_iterator var = eqvClass->vars.begin(); var != eqvClass->vars.end(); ++var ) {
      openVars[ *var ] = eqvClass->kind;
    }
  }
}

} // namespace ResolvExpr
