Ignore:
Timestamp:
Dec 16, 2020, 2:43:12 PM (3 years ago)
Author:
Fangren Yu <f37yu@…>
Branches:
ADT, arm-eh, ast-experimental, enum, forall-pointer-decay, jacob/cs343-translation, master, new-ast-unique-expr, pthread-emulation, qualifiedEnum
Children:
53449a4
Parents:
13fece5
Message:

reimplement function type and eliminate deep copy

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/AST/TypeEnvironment.cpp

    r13fece5 r3e5dd913  
    5252        for ( const auto & i : open ) {
    5353                if ( first ) { first = false; } else { out << ' '; }
    54                 out << i.first << "(" << i.second << ")";
     54                out << i.first.typeString() << "(" << i.second << ")";
    5555        }
    5656}
     
    6262                if(first) first = false;
    6363                else out << " ";
    64                 if( deterministic_output && isUnboundType(var) ) out << "[unbound]";
    65                 else out << var;
     64
     65                if( deterministic_output ) out << "[unbound]";
     66                else out << "_" << var.formal_usage << "_" << var.expr_id << "_";
     67
     68                out << var.base->name;
    6669        }
    6770        out << ")";
     
    7982}
    8083
    81 const EqvClass * TypeEnvironment::lookup( const std::string & var ) const {
     84const EqvClass * TypeEnvironment::lookup( const TypeInstType::TypeEnvKey & var ) const {
    8285        for ( ClassList::const_iterator i = env.begin(); i != env.end(); ++i ) {
    8386                if ( i->vars.find( var ) != i->vars.end() ) return &*i;
     
    106109
    107110void TypeEnvironment::add( const FunctionType::ForallList & tyDecls ) {
    108         for ( const TypeDecl * tyDecl : tyDecls ) {
     111        for ( auto & tyDecl : tyDecls ) {
    109112                env.emplace_back( tyDecl );
    110113        }
     
    119122void TypeEnvironment::writeToSubstitution( TypeSubstitution & sub ) const {
    120123        for ( const auto & clz : env ) {
    121                 std::string clzRep;
     124                TypeInstType::TypeEnvKey clzRep;
     125                bool first = true;
    122126                for ( const auto & var : clz.vars ) {
    123127                        if ( clz.bound ) {
    124128                                sub.add( var, clz.bound );
    125                         } else if ( clzRep.empty() ) {
     129                        } else if ( first ) {
    126130                                clzRep = var;
     131                                first = false;
    127132                        } else {
    128                                 sub.add( var, new TypeInstType{ clzRep, clz.data.kind } );
     133                                sub.add( var, new TypeInstType{ clzRep } );
    129134                        }
    130135                }
     
    141146        struct Occurs : public ast::WithVisitorRef<Occurs> {
    142147                bool result;
    143                 std::set< std::string > vars;
     148                std::unordered_set< TypeInstType::TypeEnvKey > vars;
    144149                const TypeEnvironment & tenv;
    145150
    146                 Occurs( const std::string & var, const TypeEnvironment & env )
     151                Occurs( const TypeInstType::TypeEnvKey & var, const TypeEnvironment & env )
    147152                : result( false ), vars(), tenv( env ) {
    148153                        if ( const EqvClass * clz = tenv.lookup( var ) ) {
     
    154159
    155160                void previsit( const TypeInstType * typeInst ) {
    156                         if ( vars.count( typeInst->name ) ) {
     161                        if ( vars.count( *typeInst ) ) {
    157162                                result = true;
    158                         } else if ( const EqvClass * clz = tenv.lookup( typeInst->name ) ) {
     163                        } else if ( const EqvClass * clz = tenv.lookup( *typeInst ) ) {
    159164                                if ( clz->bound ) {
    160165                                        clz->bound->accept( *visitor );
     
    165170
    166171        /// true if `var` occurs in `ty` under `env`
    167         bool occurs( const Type * ty, const std::string & var, const TypeEnvironment & env ) {
     172        bool occurs( const Type * ty, const TypeInstType::TypeEnvKey & var, const TypeEnvironment & env ) {
    168173                Pass<Occurs> occur{ var, env };
    169174                maybe_accept( ty, occur );
     
    280285        // remove references from bound type, so that type variables can only bind to value types
    281286        ptr<Type> target = bindTo->stripReferences();
    282         auto tyvar = open.find( typeInst->name );
     287        auto tyvar = open.find( *typeInst );
    283288        assert( tyvar != open.end() );
    284289        if ( ! tyVarCompatible( tyvar->second, target ) ) return false;
    285         if ( occurs( target, typeInst->name, *this ) ) return false;
    286 
    287         auto it = internal_lookup( typeInst->name );
     290        if ( occurs( target, *typeInst, *this ) ) return false;
     291
     292        auto it = internal_lookup( *typeInst );
    288293        if ( it != env.end() ) {
    289294                if ( it->bound ) {
     
    308313        } else {
    309314                env.emplace_back(
    310                         typeInst->name, target, widen.first && widen.second, data );
     315                        *typeInst, target, widen.first && widen.second, data );
    311316        }
    312317        return true;
     
    318323                WidenMode widen, const SymbolTable & symtab
    319324) {
    320         auto c1 = internal_lookup( var1->name );
    321         auto c2 = internal_lookup( var2->name );
     325        auto c1 = internal_lookup( *var1 );
     326        auto c2 = internal_lookup( *var2 );
    322327
    323328        // exit early if variables already bound together
     
    333338        if ( c1 != env.end() ) {
    334339                if ( c1->bound ) {
    335                         if ( occurs( c1->bound, var2->name, *this ) ) return false;
     340                        if ( occurs( c1->bound, *var2, *this ) ) return false;
    336341                        type1 = c1->bound;
    337342                }
     
    340345        if ( c2 != env.end() ) {
    341346                if ( c2->bound ) {
    342                         if ( occurs( c2->bound, var1->name, *this ) ) return false;
     347                        if ( occurs( c2->bound, *var1, *this ) ) return false;
    343348                        type2 = c2->bound;
    344349                }
     
    378383        } else if ( c1 != env.end() ) {
    379384                // var2 unbound, add to env[c1]
    380                 c1->vars.emplace( var2->name );
     385                c1->vars.emplace( *var2 );
    381386                c1->allowWidening = widen1;
    382387                c1->data.isComplete |= data.isComplete;
    383388        } else if ( c2 != env.end() ) {
    384389                // var1 unbound, add to env[c2]
    385                 c2->vars.emplace( var1->name );
     390                c2->vars.emplace( *var1 );
    386391                c2->allowWidening = widen2;
    387392                c2->data.isComplete |= data.isComplete;
    388393        } else {
    389394                // neither var bound, create new class
    390                 env.emplace_back( var1->name, var2->name, widen1 && widen2, data );
     395                env.emplace_back( *var1, *var2, widen1 && widen2, data );
    391396        }
    392397
     
    452457}
    453458
    454 TypeEnvironment::ClassList::iterator TypeEnvironment::internal_lookup( const std::string & var ) {
     459TypeEnvironment::ClassList::iterator TypeEnvironment::internal_lookup( const TypeInstType::TypeEnvKey & var ) {
    455460        for ( ClassList::iterator i = env.begin(); i != env.end(); ++i ) {
    456461                if ( i->vars.count( var ) ) return i;
Note: See TracChangeset for help on using the changeset viewer.