Changes in / [668e971a:8f9cc50]


Ignore:
Location:
src
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • src/InitTweak/GenInit.cc

    r668e971a r8f9cc50  
    246246                        }
    247247                }
    248                 return managedTypes.find( SymTab::Mangler::mangle( type ) ) != managedTypes.end();
     248                // a type is managed if it appears in the map of known managed types, or if it contains any polymorphism (is a type variable or generic type containing a type variable)
     249                return managedTypes.find( SymTab::Mangler::mangle( type ) ) != managedTypes.end() || GenPoly::isPolyType( type );
    249250        }
    250251
  • src/ResolvExpr/Resolver.cc

    r668e971a r8f9cc50  
    6666                void handlePtrType( PtrType * type );
    6767
    68           void resolveAggrInit( AggregateDecl *, InitIterator &, InitIterator & );
    69           void resolveSingleAggrInit( Declaration *, InitIterator &, InitIterator & );
     68          void resolveAggrInit( ReferenceToType *, InitIterator &, InitIterator & );
     69          void resolveSingleAggrInit( Declaration *, InitIterator &, InitIterator &, TypeSubstitution sub );
    7070          void fallbackInit( ConstructorInit * ctorInit );
    7171
     
    396396        }
    397397
    398         void Resolver::resolveSingleAggrInit( Declaration * dcl, InitIterator & init, InitIterator & initEnd ) {
     398        template< typename AggrInst >
     399        TypeSubstitution makeGenericSubstitutuion( AggrInst * inst ) {
     400                std::list< TypeDecl * > baseParams = *inst->get_baseParameters();
     401                std::list< Expression * > typeSubs = inst->get_parameters();
     402                TypeSubstitution subs( baseParams.begin(), baseParams.end(), typeSubs.begin() );
     403                return subs;
     404        }
     405
     406        ReferenceToType * isStructOrUnion( Type * type ) {
     407                if ( StructInstType * sit = dynamic_cast< StructInstType * >( type ) ) {
     408                        return sit;
     409                } else if ( UnionInstType * uit = dynamic_cast< UnionInstType * >( type ) ) {
     410                        return uit;
     411                }
     412                return nullptr;
     413        }
     414
     415        void Resolver::resolveSingleAggrInit( Declaration * dcl, InitIterator & init, InitIterator & initEnd, TypeSubstitution sub ) {
    399416                DeclarationWithType * dt = dynamic_cast< DeclarationWithType * >( dcl );
    400417                assert( dt );
    401                 initContext = dt->get_type();
     418                // need to substitute for generic types, so that casts are to concrete types
     419                initContext = dt->get_type()->clone();
     420                sub.apply( initContext );
     421
    402422                try {
    403423                        if ( init == initEnd ) return; // stop when there are no more initializers
     
    406426                } catch( SemanticError & ) {
    407427                        // need to delve deeper, if you can
    408                         if ( StructInstType * sit = dynamic_cast< StructInstType * >( dt->get_type() ) ) {
    409                                 resolveAggrInit( sit->get_baseStruct(), init, initEnd );
    410                         } else if ( UnionInstType * uit = dynamic_cast< UnionInstType * >( dt->get_type() ) ) {
    411                                 resolveAggrInit( uit->get_baseUnion(), init, initEnd );
     428                        if ( ReferenceToType * type = isStructOrUnion( initContext ) ) {
     429                                resolveAggrInit( type, init, initEnd );
    412430                        } else {
    413431                                // member is not an aggregate type, so can't go any deeper
     
    419437        }
    420438
    421         void Resolver::resolveAggrInit( AggregateDecl * aggr, InitIterator & init, InitIterator & initEnd ) {
    422                 if ( StructDecl * st = dynamic_cast< StructDecl * >( aggr ) ) {
     439        void Resolver::resolveAggrInit( ReferenceToType * inst, InitIterator & init, InitIterator & initEnd ) {
     440
     441                if ( StructInstType * sit = dynamic_cast< StructInstType * >( inst ) ) {
     442                        TypeSubstitution sub = makeGenericSubstitutuion( sit );
     443                        StructDecl * st = sit->get_baseStruct();
    423444                        // want to resolve each initializer to the members of the struct,
    424445                        // but if there are more initializers than members we should stop
    425446                        list< Declaration * >::iterator it = st->get_members().begin();
    426447                        for ( ; it != st->get_members().end(); ++it) {
    427                                 resolveSingleAggrInit( *it, init, initEnd );
     448                                resolveSingleAggrInit( *it, init, initEnd, sub );
    428449                        }
    429                 } else if ( UnionDecl * un = dynamic_cast< UnionDecl * >( aggr ) ) {
     450                } else if ( UnionInstType * uit = dynamic_cast< UnionInstType * >( inst ) ) {
     451                        TypeSubstitution sub = makeGenericSubstitutuion( sit );
     452                        UnionDecl * un = uit->get_baseUnion();
    430453                        // only resolve to the first member of a union
    431                         resolveSingleAggrInit( *un->get_members().begin(), init, initEnd );
     454                        resolveSingleAggrInit( *un->get_members().begin(), init, initEnd, sub );
    432455                } // if
    433456        }
     
    449472                                (*iter++)->accept( *this );
    450473                        }
    451                 } else if ( StructInstType * st = dynamic_cast< StructInstType * >( initContext ) ) {
    452                         resolveAggrInit( st->get_baseStruct(), iter, end );
    453                 } else if ( UnionInstType * st = dynamic_cast< UnionInstType * >( initContext ) ) {
    454                         resolveAggrInit( st->get_baseUnion(), iter, end );
     474                } else if ( ReferenceToType * type = isStructOrUnion( initContext ) ) {
     475                        resolveAggrInit( type, iter, end );
    455476                } else if ( TypeInstType * tt = dynamic_cast< TypeInstType * >( initContext ) ) {
    456477                        Type * base = tt->get_baseType()->get_base();
Note: See TracChangeset for help on using the changeset viewer.