Changeset a32b204 for translator/ResolvExpr/TypeEnvironment.cc
- Timestamp:
- May 17, 2015, 1:19:35 PM (9 years ago)
- Branches:
- ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, ctor, deferred_resn, demangler, enum, forall-pointer-decay, gc_noraii, jacob/cs343-translation, jenkins-sandbox, master, memory, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, string, with_gc
- Children:
- 0dd3a2f
- Parents:
- b87a5ed
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
translator/ResolvExpr/TypeEnvironment.cc
rb87a5ed ra32b204 1 /* 2 * This file is part of the Cforall project 3 * 4 * $Id: TypeEnvironment.cc,v 1.7 2005/08/29 20:14:16 rcbilson Exp $ 5 * 6 */ 1 // 2 // Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo 3 // 4 // The contents of this file are covered under the licence agreement in the 5 // file "LICENCE" distributed with Cforall. 6 // 7 // TypeEnvironment.cc -- 8 // 9 // Author : Richard C. Bilson 10 // Created On : Sun May 17 12:19:47 2015 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Sun May 17 12:23:36 2015 13 // Update Count : 3 14 // 7 15 8 16 #include <algorithm> … … 15 23 16 24 namespace ResolvExpr { 17 18 void printAssertionSet( const AssertionSet &assertions, std::ostream &os, int indent ) 19 { 20 for( AssertionSet::const_iterator i = assertions.begin(); i != assertions.end(); ++i ) { 21 i->first->print( os, indent ); 22 if( i->second ) { 23 os << "(used)"; 24 } else { 25 os << "(not used)"; 26 } 27 } 28 } 29 30 void printOpenVarSet( const OpenVarSet &openVars, std::ostream &os, int indent ) 31 { 32 os << std::string( indent, ' ' ); 33 for( OpenVarSet::const_iterator i = openVars.begin(); i != openVars.end(); ++i ) { 34 os << i->first << "(" << i->second << ") "; 35 } 36 } 37 38 void 39 EqvClass::initialize( const EqvClass &src, EqvClass &dest ) 40 { 41 dest.vars = src.vars; 42 dest.type = maybeClone( src.type ); 43 dest.allowWidening = src.allowWidening; 44 dest.kind = src.kind; 45 } 46 47 EqvClass::EqvClass() : type( 0 ), allowWidening( true ) 48 { 49 } 50 51 EqvClass::EqvClass( const EqvClass &other ) 52 { 53 initialize( other, *this ); 54 } 55 56 EqvClass & 57 EqvClass::operator=( const EqvClass &other ) 58 { 59 if( this == &other ) return *this; 60 delete type; 61 initialize( other, *this ); 62 return *this; 63 } 64 65 EqvClass::~EqvClass() 66 { 67 delete type; 68 } 69 70 void 71 EqvClass::print( std::ostream &os, int indent ) const 72 { 73 os << std::string( indent, ' ' ) << "( "; 74 std::copy( vars.begin(), vars.end(), std::ostream_iterator< std::string >( os, " " ) ); 75 os << ")"; 76 if( type ) { 77 os << " -> "; 78 type->print( os, indent ); 79 } 80 if( !allowWidening ) { 81 os << " (no widening)"; 82 } 83 os << std::endl; 84 } 85 86 bool 87 TypeEnvironment::lookup( const std::string &var, EqvClass &eqvClass ) const 88 { 89 for( std::list< EqvClass >::const_iterator i = env.begin(); i != env.end(); ++i ) { 90 if( i->vars.find( var ) != i->vars.end() ) { 25 void printAssertionSet( const AssertionSet &assertions, std::ostream &os, int indent ) { 26 for ( AssertionSet::const_iterator i = assertions.begin(); i != assertions.end(); ++i ) { 27 i->first->print( os, indent ); 28 if ( i->second ) { 29 os << "(used)"; 30 } else { 31 os << "(not used)"; 32 } // if 33 } // for 34 } 35 36 void printOpenVarSet( const OpenVarSet &openVars, std::ostream &os, int indent ) { 37 os << std::string( indent, ' ' ); 38 for ( OpenVarSet::const_iterator i = openVars.begin(); i != openVars.end(); ++i ) { 39 os << i->first << "(" << i->second << ") "; 40 } // for 41 } 42 43 void EqvClass::initialize( const EqvClass &src, EqvClass &dest ) { 44 dest.vars = src.vars; 45 dest.type = maybeClone( src.type ); 46 dest.allowWidening = src.allowWidening; 47 dest.kind = src.kind; 48 } 49 50 EqvClass::EqvClass() : type( 0 ), allowWidening( true ) { 51 } 52 53 EqvClass::EqvClass( const EqvClass &other ) { 54 initialize( other, *this ); 55 } 56 57 EqvClass &EqvClass::operator=( const EqvClass &other ) { 58 if ( this == &other ) return *this; 59 delete type; 60 initialize( other, *this ); 61 return *this; 62 } 63 64 EqvClass::~EqvClass() { 65 delete type; 66 } 67 68 void EqvClass::print( std::ostream &os, int indent ) const { 69 os << std::string( indent, ' ' ) << "( "; 70 std::copy( vars.begin(), vars.end(), std::ostream_iterator< std::string >( os, " " ) ); 71 os << ")"; 72 if ( type ) { 73 os << " -> "; 74 type->print( os, indent ); 75 } // if 76 if ( ! allowWidening ) { 77 os << " (no widening)"; 78 } // if 79 os << std::endl; 80 } 81 82 bool TypeEnvironment::lookup( const std::string &var, EqvClass &eqvClass ) const { 83 for ( std::list< EqvClass >::const_iterator i = env.begin(); i != env.end(); ++i ) { 84 if ( i->vars.find( var ) != i->vars.end() ) { 91 85 /// std::cout << var << " is in class "; 92 86 /// i->print( std::cout ); 93 94 95 87 eqvClass = *i; 88 return true; 89 } 96 90 /// std::cout << var << " is not in class "; 97 91 /// i->print( std::cout ); 98 } 99 return false; 100 } 101 102 void 103 TypeEnvironment::add( const EqvClass &eqvClass ) 104 { 105 std::list< EqvClass >::iterator i = env.begin(); 106 while( i != env.end() ) { 107 std::list< EqvClass >::iterator next = i; 108 next++; 109 std::set< std::string > intersection; 110 std::set_intersection( i->vars.begin(), i->vars.end(), eqvClass.vars.begin(), eqvClass.vars.end(), std::inserter( intersection, intersection.begin() ) ); 111 if( !intersection.empty() ) { 112 env.erase( i ); 113 } 114 i = next; 115 } 116 env.insert( env.end(), eqvClass ); 117 } 118 119 void 120 TypeEnvironment::add( const std::list< TypeDecl* > &tyDecls ) 121 { 122 for( std::list< TypeDecl* >::const_iterator i = tyDecls.begin(); i != tyDecls.end(); ++i ) { 123 EqvClass newClass; 124 newClass.vars.insert( (*i)->get_name() ); 125 newClass.kind = (*i)->get_kind(); 126 env.push_back( newClass ); 127 } 128 } 129 130 void 131 TypeEnvironment::makeSubstitution( TypeSubstitution &sub ) const 132 { 133 for( std::list< EqvClass >::const_iterator theClass = env.begin(); theClass != env.end(); ++theClass ) { 134 for( std::set< std::string >::const_iterator theVar = theClass->vars.begin(); theVar != theClass->vars.end(); ++theVar ) { 92 } // for 93 return false; 94 } 95 96 void TypeEnvironment::add( const EqvClass &eqvClass ) { 97 std::list< EqvClass >::iterator i = env.begin(); 98 while ( i != env.end() ) { 99 std::list< EqvClass >::iterator next = i; 100 next++; 101 std::set< std::string > intersection; 102 std::set_intersection( i->vars.begin(), i->vars.end(), eqvClass.vars.begin(), eqvClass.vars.end(), std::inserter( intersection, intersection.begin() ) ); 103 if ( ! intersection.empty() ) { 104 env.erase( i ); 105 } // if 106 i = next; 107 } // while 108 env.insert( env.end(), eqvClass ); 109 } 110 111 void TypeEnvironment::add( const std::list< TypeDecl* > &tyDecls ) { 112 for ( std::list< TypeDecl* >::const_iterator i = tyDecls.begin(); i != tyDecls.end(); ++i ) { 113 EqvClass newClass; 114 newClass.vars.insert( (*i)->get_name() ); 115 newClass.kind = (*i)->get_kind(); 116 env.push_back( newClass ); 117 } // for 118 } 119 120 void TypeEnvironment::makeSubstitution( TypeSubstitution &sub ) const { 121 for ( std::list< EqvClass >::const_iterator theClass = env.begin(); theClass != env.end(); ++theClass ) { 122 for ( std::set< std::string >::const_iterator theVar = theClass->vars.begin(); theVar != theClass->vars.end(); ++theVar ) { 135 123 /// std::cout << "adding " << *theVar; 136 if( theClass->type ) {124 if ( theClass->type ) { 137 125 /// std::cout << " bound to "; 138 126 /// theClass->type->print( std::cout ); 139 127 /// std::cout << std::endl; 140 141 } else if( theVar != theClass->vars.begin() ) {142 128 sub.add( *theVar, theClass->type ); 129 } else if ( theVar != theClass->vars.begin() ) { 130 TypeInstType *newTypeInst = new TypeInstType( Type::Qualifiers(), *theClass->vars.begin(), theClass->kind == TypeDecl::Ftype ); 143 131 /// std::cout << " bound to variable " << *theClass->vars.begin() << std::endl; 144 145 146 } 147 } 148 } 132 sub.add( *theVar, newTypeInst ); 133 delete newTypeInst; 134 } // if 135 } // for 136 } // for 149 137 /// std::cerr << "input env is:" << std::endl; 150 138 /// print( std::cerr, 8 ); 151 139 /// std::cerr << "sub is:" << std::endl; 152 140 /// sub.print( std::cerr, 8 ); 153 sub.normalize(); 154 } 155 156 void 157 TypeEnvironment::print( std::ostream &os, int indent ) const 158 { 159 for( std::list< EqvClass >::const_iterator i = env.begin(); i != env.end(); ++i ) { 160 i->print( os, indent ); 161 } 162 } 163 164 std::list< EqvClass >::iterator 165 TypeEnvironment::internal_lookup( const std::string &var ) 166 { 167 for( std::list< EqvClass >::iterator i = env.begin(); i != env.end(); ++i ) { 168 if( i->vars.find( var ) == i->vars.end() ) { 169 return i; 170 } 171 } 172 return env.end(); 173 } 174 175 void 176 TypeEnvironment::simpleCombine( const TypeEnvironment &second ) 177 { 178 env.insert( env.end(), second.env.begin(), second.env.end() ); 179 } 180 181 void 182 TypeEnvironment::combine( const TypeEnvironment &second, Type *(*combineFunc)( Type*, Type* ) ) 183 { 184 TypeEnvironment secondCopy( second ); 185 for( std::list< EqvClass >::iterator firstClass = env.begin(); firstClass != env.end(); ++firstClass ) { 186 EqvClass &newClass = *firstClass; 187 std::set< std::string > newVars; 188 for( std::set< std::string >::const_iterator var = firstClass->vars.begin(); var != firstClass->vars.end(); ++var ) { 189 std::list< EqvClass >::iterator secondClass = secondCopy.internal_lookup( *var ); 190 if( secondClass != secondCopy.env.end() ) { 191 newVars.insert( secondClass->vars.begin(), secondClass->vars.end() ); 192 if( secondClass->type ) { 193 if( newClass.type ) { 194 Type *newType = combineFunc( newClass.type, secondClass->type ); 195 delete newClass.type; 196 newClass.type = newType; 197 newClass.allowWidening = newClass.allowWidening && secondClass->allowWidening; 198 } else { 199 newClass.type = secondClass->type->clone(); 200 newClass.allowWidening = secondClass->allowWidening; 201 } 202 } 203 secondCopy.env.erase( secondClass ); 204 } 205 } 206 newClass.vars.insert( newVars.begin(), newVars.end() ); 207 } 208 for( std::list< EqvClass >::iterator secondClass = secondCopy.env.begin(); secondClass != secondCopy.env.end(); ++secondClass ) { 209 env.push_back( *secondClass ); 210 } 211 } 212 213 void 214 TypeEnvironment::extractOpenVars( OpenVarSet &openVars ) const 215 { 216 for( std::list< EqvClass >::const_iterator eqvClass = env.begin(); eqvClass != env.end(); ++eqvClass ) { 217 for( std::set< std::string >::const_iterator var = eqvClass->vars.begin(); var != eqvClass->vars.end(); ++var ) { 218 openVars[ *var ] = eqvClass->kind; 219 } 220 } 221 } 141 sub.normalize(); 142 } 143 144 void TypeEnvironment::print( std::ostream &os, int indent ) const { 145 for ( std::list< EqvClass >::const_iterator i = env.begin(); i != env.end(); ++i ) { 146 i->print( os, indent ); 147 } // for 148 } 149 150 std::list< EqvClass >::iterator TypeEnvironment::internal_lookup( const std::string &var ) { 151 for ( std::list< EqvClass >::iterator i = env.begin(); i != env.end(); ++i ) { 152 if ( i->vars.find( var ) == i->vars.end() ) { 153 return i; 154 } // if 155 } // for 156 return env.end(); 157 } 158 159 void TypeEnvironment::simpleCombine( const TypeEnvironment &second ) { 160 env.insert( env.end(), second.env.begin(), second.env.end() ); 161 } 162 163 void TypeEnvironment::combine( const TypeEnvironment &second, Type *(*combineFunc)( Type*, Type* ) ) { 164 TypeEnvironment secondCopy( second ); 165 for ( std::list< EqvClass >::iterator firstClass = env.begin(); firstClass != env.end(); ++firstClass ) { 166 EqvClass &newClass = *firstClass; 167 std::set< std::string > newVars; 168 for ( std::set< std::string >::const_iterator var = firstClass->vars.begin(); var != firstClass->vars.end(); ++var ) { 169 std::list< EqvClass >::iterator secondClass = secondCopy.internal_lookup( *var ); 170 if ( secondClass != secondCopy.env.end() ) { 171 newVars.insert( secondClass->vars.begin(), secondClass->vars.end() ); 172 if ( secondClass->type ) { 173 if ( newClass.type ) { 174 Type *newType = combineFunc( newClass.type, secondClass->type ); 175 delete newClass.type; 176 newClass.type = newType; 177 newClass.allowWidening = newClass.allowWidening && secondClass->allowWidening; 178 } else { 179 newClass.type = secondClass->type->clone(); 180 newClass.allowWidening = secondClass->allowWidening; 181 } // if 182 } // if 183 secondCopy.env.erase( secondClass ); 184 } // if 185 } // for 186 newClass.vars.insert( newVars.begin(), newVars.end() ); 187 } // for 188 for ( std::list< EqvClass >::iterator secondClass = secondCopy.env.begin(); secondClass != secondCopy.env.end(); ++secondClass ) { 189 env.push_back( *secondClass ); 190 } // for 191 } 192 193 void TypeEnvironment::extractOpenVars( OpenVarSet &openVars ) const { 194 for ( std::list< EqvClass >::const_iterator eqvClass = env.begin(); eqvClass != env.end(); ++eqvClass ) { 195 for ( std::set< std::string >::const_iterator var = eqvClass->vars.begin(); var != eqvClass->vars.end(); ++var ) { 196 openVars[ *var ] = eqvClass->kind; 197 } // for 198 } // for 199 } 222 200 223 201 } // namespace ResolvExpr 202 203 // Local Variables: // 204 // tab-width: 4 // 205 // mode: c++ // 206 // compile-command: "make install" // 207 // End: //
Note: See TracChangeset
for help on using the changeset viewer.