// // Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo // // The contents of this file are covered under the licence agreement in the // file "LICENCE" distributed with Cforall. // // TypeEnvironment.cc -- // // Author : Richard C. Bilson // Created On : Sun May 17 12:19:47 2015 // Last Modified By : Peter A. Buhr // Last Modified On : Sun May 17 12:23:36 2015 // Update Count : 3 // #include // for assert #include // for copy, set_intersection #include // for ostream_iterator, insert_iterator #include // for pair, move #include "Common/utility.h" // for maybeClone #include "SynTree/Type.h" // for Type, FunctionType, Type::Fora... #include "SynTree/TypeSubstitution.h" // for TypeSubstitution #include "TypeEnvironment.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.isUsed ) { os << "(used)"; } else { os << "(not used)"; } // if } // for } 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 << ") "; } // for } void EqvClass::initialize( const EqvClass &src, EqvClass &dest ) { initialize( src, dest, src.type ); } void EqvClass::initialize( const EqvClass &src, EqvClass &dest, const Type *ty ) { dest.vars = src.vars; dest.type = maybeClone( ty ); dest.allowWidening = src.allowWidening; dest.data = src.data; } EqvClass::EqvClass() : type( nullptr ), allowWidening( true ) { } EqvClass::EqvClass( const EqvClass &other ) { initialize( other, *this ); } EqvClass::EqvClass( const EqvClass &other, const Type *ty ) { initialize( other, *this, ty ); } 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, Indenter indent ) const { os << "( "; std::copy( vars.begin(), vars.end(), std::ostream_iterator< std::string >( os, " " ) ); os << ")"; if ( type ) { os << " -> "; type->print( os, indent+1 ); } // if if ( ! allowWidening ) { os << " (no widening)"; } // if os << std::endl; } const EqvClass* TypeEnvironment::lookup( const std::string &var ) 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 ); return &*i; } /// std::cout << var << " is not in class "; /// i->print( std::cout ); } // for return nullptr; } /// Removes any class from env that intersects eqvClass void filterOverlappingClasses( std::list &env, const EqvClass &eqvClass ) { for ( auto i = env.begin(); i != env.end(); ) { auto next = i; ++next; std::set 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; } } void TypeEnvironment::add( const EqvClass &eqvClass ) { filterOverlappingClasses( env, eqvClass ); env.push_back( eqvClass ); } void TypeEnvironment::add( EqvClass &&eqvClass ) { filterOverlappingClasses( env, eqvClass ); env.push_back( std::move(eqvClass) ); } void TypeEnvironment::add( const Type::ForallList &tyDecls ) { for ( Type::ForallList::const_iterator i = tyDecls.begin(); i != tyDecls.end(); ++i ) { EqvClass newClass; newClass.vars.insert( (*i)->get_name() ); newClass.data = TypeDecl::Data{ (*i) }; env.push_back( newClass ); } // for } void TypeEnvironment::add( const TypeSubstitution & sub ) { EqvClass newClass; for ( auto p : sub ) { newClass.vars.insert( p.first ); newClass.type = p.second->clone(); newClass.allowWidening = false; // Minimal assumptions. Not technically correct, but might be good enough, and // is the best we can do at the moment since information is lost in the // transition to TypeSubstitution newClass.data = TypeDecl::Data{ TypeDecl::Dtype, false }; add( 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::cerr << "adding " << *theVar; if ( theClass->type ) { /// std::cerr << " bound to "; /// theClass->type->print( std::cerr ); /// std::cerr << std::endl; sub.add( *theVar, theClass->type ); } else if ( theVar != theClass->vars.begin() ) { TypeInstType *newTypeInst = new TypeInstType( Type::Qualifiers(), *theClass->vars.begin(), theClass->data.kind == TypeDecl::Ftype ); /// std::cerr << " bound to variable " << *theClass->vars.begin() << std::endl; sub.add( *theVar, newTypeInst ); delete newTypeInst; } // if } // for } // for /// 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, Indenter indent ) const { for ( const EqvClass & theClass : env ) { theClass.print( os, indent ); } // for } 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; } // if } // for 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; } // if } // if secondCopy.env.erase( secondClass ); } // if } // for newClass.vars.insert( newVars.begin(), newVars.end() ); } // for for ( std::list< EqvClass >::iterator secondClass = secondCopy.env.begin(); secondClass != secondCopy.env.end(); ++secondClass ) { env.push_back( *secondClass ); } // for } 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->data; } // for } // for } void TypeEnvironment::addActual( const TypeEnvironment& actualEnv, OpenVarSet& openVars ) { for ( const EqvClass& c : actualEnv ) { EqvClass c2 = c; c2.allowWidening = false; for ( const std::string& var : c2.vars ) { openVars[ var ] = c2.data; } env.push_back( std::move(c2) ); } } std::ostream & operator<<( std::ostream & out, const TypeEnvironment & env ) { env.print( out ); return out; } } // namespace ResolvExpr // Local Variables: // // tab-width: 4 // // mode: c++ // // compile-command: "make install" // // End: //