Changeset ffad73a for src/GenPoly/Box.cc


Ignore:
Timestamp:
Dec 15, 2015, 4:57:31 PM (10 years ago)
Author:
Aaron Moss <a3moss@…>
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:
5f6c42c
Parents:
78dd0da
Message:

Refactored isPolyType and friends to account for polymorphic generic types

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/GenPoly/Box.cc

    r78dd0da rffad73a  
    155155                        // with the same mangled name, so we need to further mangle the names.
    156156                        for ( std::list< DeclarationWithType *>::iterator retval = function->get_returnVals().begin(); retval != function->get_returnVals().end(); ++retval ) {
    157                                 if ( isPolyVal( (*retval)->get_type(), tyVars ) ) {
     157                                if ( isPolyType( (*retval)->get_type(), tyVars ) ) {
    158158                                        name << "P";
    159159                                } else {
     
    164164                        std::list< DeclarationWithType *> &paramList = function->get_parameters();
    165165                        for ( std::list< DeclarationWithType *>::iterator arg = paramList.begin(); arg != paramList.end(); ++arg ) {
    166                                 if ( isPolyVal( (*arg)->get_type(), tyVars ) ) {
     166                                if ( isPolyType( (*arg)->get_type(), tyVars ) ) {
    167167                                        name << "P";
    168168                                } else {
     
    326326                }
    327327
    328                 TypeInstType *isPolyType( Type *type, const TypeSubstitution *env, const TyVarMap &tyVars ) {
    329                         if ( TypeInstType *typeInst = dynamic_cast< TypeInstType *>( type ) ) {
    330                                 if ( env ) {
    331                                         if ( Type *newType = env->lookup( typeInst->get_name() ) ) {
    332                                                 return isPolyType( newType, env, tyVars );
    333                                         } // if
    334                                 } // if
    335                                 if ( tyVars.find( typeInst->get_name() ) != tyVars.end() ) {
    336                                         return typeInst;
    337                                 } else {
    338                                         return 0;
    339                                 } // if
    340                         } else {
    341                                 return 0;
    342                         } // if
    343                 }
    344 
    345328                Expression *Pass1::addRetParam( ApplicationExpr *appExpr, FunctionType *function, Type *retType, std::list< Expression *>::iterator &arg ) {
    346329                        if ( useRetval ) {
     
    351334                                ObjectDecl *newObj = makeTemporary( retType->clone() );
    352335                                Expression *paramExpr = new VariableExpr( newObj );
    353                                 if ( ! isPolyType( newObj->get_type(), env, scopeTyVars ) ) {
     336                                if ( ! isPolyType( newObj->get_type(), scopeTyVars, env ) ) {
    354337                                        paramExpr = new AddressExpr( paramExpr );
    355338                                } // if
     
    377360                Expression *Pass1::applyAdapter( ApplicationExpr *appExpr, FunctionType *function, std::list< Expression *>::iterator &arg, const TyVarMap &tyVars ) {
    378361                        Expression *ret = appExpr;
    379                         if ( ! function->get_returnVals().empty() && isPolyVal( function->get_returnVals().front()->get_type(), tyVars ) ) {
     362                        if ( ! function->get_returnVals().empty() && isPolyType( function->get_returnVals().front()->get_type(), tyVars ) ) {
    380363                                ret = addRetParam( appExpr, function, function->get_returnVals().front()->get_type(), arg );
    381364                        } // if
     
    391374                void Pass1::boxParam( Type *param, Expression *&arg, const TyVarMap &exprTyVars ) {
    392375                        assert( ! arg->get_results().empty() );
    393 ///   if ( ! dynamic_cast< PointerType *>( arg->get_results().front() ) ) {
     376//   if ( ! dynamic_cast< PointerType *>( arg->get_results().front() ) ) {
    394377                        TypeInstType *typeInst = dynamic_cast< TypeInstType *>( param );
    395378                        if ( typeInst && exprTyVars.find( typeInst->get_name() ) != exprTyVars.end() ) {
     
    411394                                } // if
    412395                        } // if
    413 ///   }
     396//   }
    414397                }
    415398
     
    469452                        // actually make the adapter type
    470453                        FunctionType *adapter = adaptee->clone();
    471                         if ( ! adapter->get_returnVals().empty() && isPolyVal( adapter->get_returnVals().front()->get_type(), tyVars ) ) {
     454                        if ( ! adapter->get_returnVals().empty() && isPolyType( adapter->get_returnVals().front()->get_type(), tyVars ) ) {
    472455                                makeRetParm( adapter );
    473456                        } // if
     
    479462                        assert( param );
    480463                        assert( arg );
    481 ///   std::cout << "arg type is ";
    482 ///   arg->get_type()->print( std::cout );
    483 ///   std::cout << "param type is ";
    484 ///   param->get_type()->print( std::cout );
    485 ///   std::cout << " tyVars are: ";
    486 ///   printTyVarMap( std::cout, tyVars );
    487                         if ( isPolyVal( realParam->get_type(), tyVars ) ) {
    488 ///     if ( dynamic_cast< PointerType *>( arg->get_type() ) ) {
    489 ///       return new CastExpr( new VariableExpr( param ), arg->get_type()->clone() );
    490 ///     } else {
     464                        if ( isPolyType( realParam->get_type(), tyVars ) ) {
     465//     if ( dynamic_cast< PointerType *>( arg->get_type() ) ) {
     466//       return new CastExpr( new VariableExpr( param ), arg->get_type()->clone() );
     467//     } else {
    491468                                if ( dynamic_cast<TypeInstType *>(arg->get_type()) == NULL ) {
    492469                                        UntypedExpr *deref = new UntypedExpr( new NameExpr( "*?" ) );
     
    495472                                        return deref;
    496473                                } // if
    497 ///     }
     474//     }
    498475                        } // if
    499476                        return new VariableExpr( param );
     
    542519                                addAdapterParams( adapteeApp, arg, param, adapterType->get_parameters().end(), realParam, tyVars );
    543520                                bodyStmt = new ExprStmt( noLabels, adapteeApp );
    544                         } else if ( isPolyVal( adaptee->get_returnVals().front()->get_type(), tyVars ) ) {
     521                        } else if ( isPolyType( adaptee->get_returnVals().front()->get_type(), tyVars ) ) {
    545522                                if ( (*param)->get_name() == "" ) {
    546523                                        (*param)->set_name( "_ret" );
     
    614591                } // passAdapters
    615592
    616                 TypeInstType *isPolyPtr( Type *type, const TypeSubstitution *env, const TyVarMap &tyVars ) {
    617                         if ( PointerType *ptr = dynamic_cast< PointerType *>( type ) ) {
    618                                 return isPolyType( ptr->get_base(), env, tyVars );
    619                         } else if ( env ) {
    620                                 if ( TypeInstType *typeInst = dynamic_cast< TypeInstType *>( type ) ) {
    621                                         if ( Type *newType = env->lookup( typeInst->get_name() ) ) {
    622                                                 return isPolyPtr( newType, env, tyVars );
    623                                         } // if
    624                                 } // if
    625                         } // if
    626                         return 0;
    627                 }
    628 
    629                 TypeInstType *isPolyPtrPtr( Type *type, const TypeSubstitution *env, const TyVarMap &tyVars ) {
    630                         if ( PointerType *ptr = dynamic_cast< PointerType *>( type ) ) {
    631                                 return isPolyPtr( ptr->get_base(), env, tyVars );
    632                         } else if ( env ) {
    633                                 if ( TypeInstType *typeInst = dynamic_cast< TypeInstType *>( type ) ) {
    634                                         if ( Type *newType = env->lookup( typeInst->get_name() ) ) {
    635                                                 return isPolyPtrPtr( newType, env, tyVars );
    636                                         } // if
    637                                 } // if
    638                         } // if
    639                         return 0;
    640                 }
    641 
    642593                Expression *makeIncrDecrExpr( ApplicationExpr *appExpr, Type *polyType, bool isIncr ) {
    643594                        NameExpr *opExpr;
     
    670621                                                assert( ! appExpr->get_results().empty() );
    671622                                                assert( appExpr->get_args().size() == 2 );
    672                                                 TypeInstType *typeInst1 = isPolyPtr( appExpr->get_args().front()->get_results().front(), env, scopeTyVars );
    673                                                 TypeInstType *typeInst2 = isPolyPtr( appExpr->get_args().back()->get_results().front(), env, scopeTyVars );
    674                                                 assert( ! typeInst1 || ! typeInst2 );
     623                                                Type *baseType1 = isPolyPtr( appExpr->get_args().front()->get_results().front(), scopeTyVars, env );
     624                                                Type *baseType2 = isPolyPtr( appExpr->get_args().back()->get_results().front(), scopeTyVars, env );
     625                                                assert( ! baseType1 || ! baseType2 );
    675626                                                UntypedExpr *ret = 0;
    676                                                 if ( typeInst1 || typeInst2 ) {
     627                                                if ( baseType1 || baseType2 ) {
    677628                                                        ret = new UntypedExpr( new NameExpr( "?+?" ) );
    678629                                                } // if
    679                                                 if ( typeInst1 ) {
     630                                                if ( baseType1 ) {
    680631                                                        UntypedExpr *multiply = new UntypedExpr( new NameExpr( "?*?" ) );
    681632                                                        multiply->get_args().push_back( appExpr->get_args().back() );
    682                                                         multiply->get_args().push_back( new NameExpr( sizeofName( typeInst1 ) ) );
     633                                                        multiply->get_args().push_back( new NameExpr( sizeofName( baseType1 ) ) );
    683634                                                        ret->get_args().push_back( appExpr->get_args().front() );
    684635                                                        ret->get_args().push_back( multiply );
    685                                                 } else if ( typeInst2 ) {
     636                                                } else if ( baseType2 ) {
    686637                                                        UntypedExpr *multiply = new UntypedExpr( new NameExpr( "?*?" ) );
    687638                                                        multiply->get_args().push_back( appExpr->get_args().front() );
    688                                                         multiply->get_args().push_back( new NameExpr( sizeofName( typeInst2 ) ) );
     639                                                        multiply->get_args().push_back( new NameExpr( sizeofName( baseType2 ) ) );
    689640                                                        ret->get_args().push_back( multiply );
    690641                                                        ret->get_args().push_back( appExpr->get_args().back() );
    691642                                                } // if
    692                                                 if ( typeInst1 || typeInst2 ) {
     643                                                if ( baseType1 || baseType2 ) {
    693644                                                        ret->get_results().push_front( appExpr->get_results().front()->clone() );
    694645                                                        if ( appExpr->get_env() ) {
     
    703654                                                assert( ! appExpr->get_results().empty() );
    704655                                                assert( ! appExpr->get_args().empty() );
    705                                                 if ( isPolyType( appExpr->get_results().front(), env, scopeTyVars ) ) {
     656                                                if ( isPolyType( appExpr->get_results().front(), scopeTyVars, env ) ) {
    706657                                                        Expression *ret = appExpr->get_args().front();
    707658                                                        delete ret->get_results().front();
     
    718669                                                assert( ! appExpr->get_results().empty() );
    719670                                                assert( appExpr->get_args().size() == 1 );
    720                                                 if ( TypeInstType *typeInst = isPolyPtr( appExpr->get_results().front(), env, scopeTyVars ) ) {
     671                                                if ( Type *baseType = isPolyPtr( appExpr->get_results().front(), scopeTyVars, env ) ) {
    721672                                                        Type *tempType = appExpr->get_results().front()->clone();
    722673                                                        if ( env ) {
     
    732683                                                                assignExpr->get_args().push_back( appExpr->get_args().front()->clone() );
    733684                                                        } // if
    734                                                         CommaExpr *firstComma = new CommaExpr( assignExpr, makeIncrDecrExpr( appExpr, typeInst, varExpr->get_var()->get_name() == "?++" ) );
     685                                                        CommaExpr *firstComma = new CommaExpr( assignExpr, makeIncrDecrExpr( appExpr, baseType, varExpr->get_var()->get_name() == "?++" ) );
    735686                                                        return new CommaExpr( firstComma, tempExpr );
    736687                                                } // if
     
    738689                                                assert( ! appExpr->get_results().empty() );
    739690                                                assert( appExpr->get_args().size() == 1 );
    740                                                 if ( TypeInstType *typeInst = isPolyPtr( appExpr->get_results().front(), env, scopeTyVars ) ) {
    741                                                         return makeIncrDecrExpr( appExpr, typeInst, varExpr->get_var()->get_name() == "++?" );
     691                                                if ( Type *baseType = isPolyPtr( appExpr->get_results().front(), scopeTyVars, env ) ) {
     692                                                        return makeIncrDecrExpr( appExpr, baseType, varExpr->get_var()->get_name() == "++?" );
    742693                                                } // if
    743694                                        } else if ( varExpr->get_var()->get_name() == "?+?" || varExpr->get_var()->get_name() == "?-?" ) {
    744695                                                assert( ! appExpr->get_results().empty() );
    745696                                                assert( appExpr->get_args().size() == 2 );
    746                                                 TypeInstType *typeInst1 = isPolyPtr( appExpr->get_args().front()->get_results().front(), env, scopeTyVars );
    747                                                 TypeInstType *typeInst2 = isPolyPtr( appExpr->get_args().back()->get_results().front(), env, scopeTyVars );
    748                                                 if ( typeInst1 && typeInst2 ) {
     697                                                Type *baseType1 = isPolyPtr( appExpr->get_args().front()->get_results().front(), scopeTyVars, env );
     698                                                Type *baseType2 = isPolyPtr( appExpr->get_args().back()->get_results().front(), scopeTyVars, env );
     699                                                if ( baseType1 && baseType2 ) {
    749700                                                        UntypedExpr *divide = new UntypedExpr( new NameExpr( "?/?" ) );
    750701                                                        divide->get_args().push_back( appExpr );
    751                                                         divide->get_args().push_back( new NameExpr( sizeofName( typeInst1 ) ) );
     702                                                        divide->get_args().push_back( new NameExpr( sizeofName( baseType1 ) ) );
    752703                                                        divide->get_results().push_front( appExpr->get_results().front()->clone() );
    753704                                                        if ( appExpr->get_env() ) {
     
    756707                                                        } // if
    757708                                                        return divide;
    758                                                 } else if ( typeInst1 ) {
     709                                                } else if ( baseType1 ) {
    759710                                                        UntypedExpr *multiply = new UntypedExpr( new NameExpr( "?*?" ) );
    760711                                                        multiply->get_args().push_back( appExpr->get_args().back() );
    761                                                         multiply->get_args().push_back( new NameExpr( sizeofName( typeInst1 ) ) );
     712                                                        multiply->get_args().push_back( new NameExpr( sizeofName( baseType1 ) ) );
    762713                                                        appExpr->get_args().back() = multiply;
    763                                                 } else if ( typeInst2 ) {
     714                                                } else if ( baseType2 ) {
    764715                                                        UntypedExpr *multiply = new UntypedExpr( new NameExpr( "?*?" ) );
    765716                                                        multiply->get_args().push_back( appExpr->get_args().front() );
    766                                                         multiply->get_args().push_back( new NameExpr( sizeofName( typeInst2 ) ) );
     717                                                        multiply->get_args().push_back( new NameExpr( sizeofName( baseType2 ) ) );
    767718                                                        appExpr->get_args().front() = multiply;
    768719                                                } // if
     
    770721                                                assert( ! appExpr->get_results().empty() );
    771722                                                assert( appExpr->get_args().size() == 2 );
    772                                                 TypeInstType *typeInst = isPolyPtr( appExpr->get_results().front(), env, scopeTyVars );
    773                                                 if ( typeInst ) {
     723                                                Type *baseType = isPolyPtr( appExpr->get_results().front(), scopeTyVars, env );
     724                                                if ( baseType ) {
    774725                                                        UntypedExpr *multiply = new UntypedExpr( new NameExpr( "?*?" ) );
    775726                                                        multiply->get_args().push_back( appExpr->get_args().back() );
    776                                                         multiply->get_args().push_back( new NameExpr( sizeofName( typeInst ) ) );
     727                                                        multiply->get_args().push_back( new NameExpr( sizeofName( baseType ) ) );
    777728                                                        appExpr->get_args().back() = multiply;
    778729                                                } // if
     
    841792
    842793                Expression *Pass1::mutate( UntypedExpr *expr ) {
    843                         if ( ! expr->get_results().empty() && isPolyType( expr->get_results().front(), env, scopeTyVars ) ) {
     794                        if ( ! expr->get_results().empty() && isPolyType( expr->get_results().front(), scopeTyVars, env ) ) {
    844795                                if ( NameExpr *name = dynamic_cast< NameExpr *>( expr->get_function() ) ) {
    845796                                        if ( name->get_name() == "*?" ) {
     
    857808                        assert( ! addrExpr->get_arg()->get_results().empty() );
    858809                        addrExpr->set_arg( mutateExpression( addrExpr->get_arg() ) );
    859                         if ( isPolyType( addrExpr->get_arg()->get_results().front(), env, scopeTyVars ) ) {
     810                        if ( isPolyType( addrExpr->get_arg()->get_results().front(), scopeTyVars, env ) ) {
    860811                                Expression *ret = addrExpr->get_arg();
    861812                                delete ret->get_results().front();
     
    11041055                Statement *Pass3::mutate( DeclStmt *declStmt ) {
    11051056                        if ( ObjectDecl *objectDecl = dynamic_cast< ObjectDecl *>( declStmt->get_decl() ) ) {
    1106                                 if ( isPolyVal( objectDecl->get_type(), scopeTyVars ) ) {
     1057                                if ( isPolyType( objectDecl->get_type(), scopeTyVars ) ) {
    11071058                                        // change initialization of a polymorphic value object
    11081059                                        // to allocate storage with alloca
    1109                                         TypeInstType *typeInst = dynamic_cast< TypeInstType *>( objectDecl->get_type() );
    1110                                         assert( typeInst );
     1060                                        Type *declType = objectDecl->get_type();
    11111061                                        UntypedExpr *alloc = new UntypedExpr( new NameExpr( "__builtin_alloca" ) );
    1112                                         alloc->get_args().push_back( new NameExpr( sizeofName( typeInst ) ) );
     1062                                        alloc->get_args().push_back( new NameExpr( sizeofName( declType ) ) );
    11131063
    11141064                                        delete objectDecl->get_init();
Note: See TracChangeset for help on using the changeset viewer.