Changeset 4f748c5 for src/GenPoly


Ignore:
Timestamp:
Nov 2, 2017, 11:45:10 AM (8 years ago)
Author:
Rob Schluntz <rschlunt@…>
Branches:
ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
Children:
6de43b6
Parents:
b1e68d03 (diff), fde89cf6 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge branch 'fix-missing-cast-warning'

Location:
src/GenPoly
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • src/GenPoly/Box.cc

    rb1e68d03 r4f748c5  
    734734                                Type * newType = param->clone();
    735735                                if ( env ) env->apply( newType );
    736                                 ObjectDecl *newObj = new ObjectDecl( tempNamer.newName(), Type::StorageClasses(), LinkageSpec::C, 0, newType, 0 );
     736                                ObjectDecl *newObj = ObjectDecl::newObject( tempNamer.newName(), newType, nullptr );
    737737                                newObj->get_type()->get_qualifiers() = Type::Qualifiers(); // TODO: is this right???
    738738                                stmtsToAddBefore.push_back( new DeclStmt( noLabels, newObj ) );
     
    745745                }
    746746
     747                // find instances of polymorphic type parameters
     748                struct PolyFinder {
     749                        const TyVarMap * tyVars = nullptr;
     750                        bool found = false;
     751
     752                        void previsit( TypeInstType * t ) {
     753                                if ( isPolyType( t, *tyVars ) ) {
     754                                        found = true;
     755                                }
     756                        }
     757                };
     758
     759                // true if there is an instance of a polymorphic type parameter in t
     760                bool hasPolymorphism( Type * t, const TyVarMap &tyVars ) {
     761                        PassVisitor<PolyFinder> finder;
     762                        finder.pass.tyVars = &tyVars;
     763                        maybeAccept( t, finder );
     764                        return finder.pass.found;
     765                }
     766
    747767                /// cast parameters to polymorphic functions so that types are replaced with
    748768                /// void * if they are type parameters in the formal type.
    749769                /// this gets rid of warnings from gcc.
    750770                void addCast( Expression *&actual, Type *formal, const TyVarMap &tyVars ) {
    751                         if ( getFunctionType( formal ) ) {
     771                        // type contains polymorphism, but isn't exactly a polytype, in which case it
     772                        // has some real actual type (e.g. unsigned int) and casting to void * is wrong
     773                        if ( hasPolymorphism( formal, tyVars ) && ! isPolyType( formal, tyVars ) ) {
    752774                                Type * newType = formal->clone();
    753775                                newType = ScrubTyVars::scrub( newType, tyVars );
     
    757779
    758780                void Pass1::boxParams( ApplicationExpr *appExpr, FunctionType *function, std::list< Expression *>::iterator &arg, const TyVarMap &exprTyVars ) {
    759                         for ( std::list< DeclarationWithType *>::const_iterator param = function->get_parameters().begin(); param != function->get_parameters().end(); ++param, ++arg ) {
    760                                 assertf( arg != appExpr->get_args().end(), "boxParams: missing argument for param %s to %s in %s", toString( *param ).c_str(), toString( function ).c_str(), toString( appExpr ).c_str() );
     781                        for ( std::list< DeclarationWithType *>::const_iterator param = function->get_parameters().begin(); param != function->parameters.end(); ++param, ++arg ) {
     782                                assertf( arg != appExpr->args.end(), "boxParams: missing argument for param %s to %s in %s", toString( *param ).c_str(), toString( function ).c_str(), toString( appExpr ).c_str() );
    761783                                addCast( *arg, (*param)->get_type(), exprTyVars );
    762784                                boxParam( (*param)->get_type(), *arg, exprTyVars );
     
    767789                        std::list< Expression *>::iterator cur = arg;
    768790                        for ( Type::ForallList::iterator tyVar = functionType->get_forall().begin(); tyVar != functionType->get_forall().end(); ++tyVar ) {
    769                                 for ( std::list< DeclarationWithType *>::iterator assert = (*tyVar)->get_assertions().begin(); assert != (*tyVar)->get_assertions().end(); ++assert ) {
     791                                for ( std::list< DeclarationWithType *>::iterator assert = (*tyVar)->assertions.begin(); assert != (*tyVar)->assertions.end(); ++assert ) {
    770792                                        InferredParams::const_iterator inferParam = appExpr->get_inferParams().find( (*assert)->get_uniqueId() );
    771                                         if ( inferParam == appExpr->get_inferParams().end() ) {
    772                                                 std::cerr << "looking for assertion: " << (*assert) << std::endl << appExpr << std::endl;
    773                                         }
    774                                         assertf( inferParam != appExpr->get_inferParams().end(), "NOTE: Explicit casts of polymorphic functions to compatible monomorphic functions are currently unsupported" );
     793                                        assertf( inferParam != appExpr->get_inferParams().end(), "addInferredParams missing inferred parameter: %s in: %s", toString( *assert ).c_str(), toString( appExpr ).c_str() );
    775794                                        Expression *newExpr = inferParam->second.expr->clone();
    776795                                        addCast( newExpr, (*assert)->get_type(), tyVars );
     
    782801
    783802                void makeRetParm( FunctionType *funcType ) {
    784                         DeclarationWithType *retParm = funcType->get_returnVals().front();
     803                        DeclarationWithType *retParm = funcType->returnVals.front();
    785804
    786805                        // make a new parameter that is a pointer to the type of the old return value
     
    795814                        // actually make the adapter type
    796815                        FunctionType *adapter = adaptee->clone();
    797 //                      if ( ! adapter->get_returnVals().empty() && isPolyType( adapter->get_returnVals().front()->get_type(), tyVars ) ) {
    798816                        if ( isDynRet( adapter, tyVars ) ) {
    799817                                makeRetParm( adapter );
  • src/GenPoly/InstantiateGeneric.cc

    rb1e68d03 r4f748c5  
    238238                                        assertf( false, "Ttype parameters are not currently allowed as parameters to generic types." );
    239239                                        break;
    240                                 case TypeDecl::Any:
    241                                         assertf( false, "otype parameters handled by baseParam->isComplete()." );
     240                                default:
     241                                        assertf( false, "Unhandled type parameter kind" );
    242242                                        break;
    243243                        }
  • src/GenPoly/ScrubTyVars.cc

    rb1e68d03 r4f748c5  
    4040                if ( tyVar != tyVars->end() ) {
    4141                        switch ( tyVar->second.kind ) {
    42                           case TypeDecl::Any:
    4342                          case TypeDecl::Dtype:
    4443                          case TypeDecl::Ttype:
  • src/GenPoly/Specialize.cc

    rb1e68d03 r4f748c5  
    4545        struct Specialize final : public WithTypeSubstitution, public WithStmtsToAdd, public WithVisitorRef<Specialize> {
    4646                Expression * postmutate( ApplicationExpr *applicationExpr );
    47                 Expression * postmutate( AddressExpr *castExpr );
    4847                Expression * postmutate( CastExpr *castExpr );
    4948
    5049                void handleExplicitParams( ApplicationExpr *appExpr );
    5150                Expression * createThunkFunction( FunctionType *funType, Expression *actual, InferredParams *inferParams );
    52                 Expression * doSpecialization( Type *formalType, Expression *actual, InferredParams *inferParams = nullptr );
     51                Expression * doSpecialization( Type *formalType, Expression *actual, InferredParams *inferParams );
    5352
    5453                std::string paramPrefix = "_p";
     
    6766                                if ( ! boundType ) continue;
    6867                                if ( TypeInstType *typeInst = dynamic_cast< TypeInstType* >( boundType ) ) {
     68                                        // bound to another type variable
    6969                                        if ( closedVars.find( typeInst->get_name() ) == closedVars.end() ) {
     70                                                // bound to a closed variable => must specialize
    7071                                                return true;
    7172                                        } // if
    7273                                } else {
     74                                        // variable is bound to a concrete type => must specialize
    7375                                        return true;
    7476                                } // if
    7577                        } // for
     78                        // none of the type variables are bound
    7679                        return false;
    7780                } else {
     81                        // no env
    7882                        return false;
    7983                } // if
     
    131135                        if ( functionParameterSize( fftype ) != functionParameterSize( aftype ) ) return false;
    132136                        // tuple-parameter sizes are the same, but actual parameter sizes differ - must tuple specialize
    133                         if ( fftype->get_parameters().size() != aftype->get_parameters().size() ) return true;
     137                        if ( fftype->parameters.size() != aftype->parameters.size() ) return true;
    134138                        // total parameter size can be the same, while individual parameters can have different structure
    135                         for ( auto params : group_iterate( fftype->get_parameters(), aftype->get_parameters() ) ) {
     139                        for ( auto params : group_iterate( fftype->parameters, aftype->parameters ) ) {
    136140                                DeclarationWithType * formal = std::get<0>(params);
    137141                                DeclarationWithType * actual = std::get<1>(params);
     
    150154                if ( needsSpecialization( formalType, actual->get_result(), env ) ) {
    151155                        if ( FunctionType *funType = getFunctionType( formalType ) ) {
    152                                 ApplicationExpr *appExpr;
    153                                 VariableExpr *varExpr;
    154                                 if ( ( appExpr = dynamic_cast<ApplicationExpr*>( actual ) ) ) {
     156                                if ( ApplicationExpr * appExpr = dynamic_cast<ApplicationExpr*>( actual ) ) {
    155157                                        return createThunkFunction( funType, appExpr->get_function(), inferParams );
    156                                 } else if ( ( varExpr = dynamic_cast<VariableExpr*>( actual ) ) ) {
     158                                } else if ( VariableExpr * varExpr = dynamic_cast<VariableExpr*>( actual ) ) {
    157159                                        return createThunkFunction( funType, varExpr, inferParams );
    158160                                } else {
     
    323325        }
    324326
    325         Expression * Specialize::postmutate( AddressExpr *addrExpr ) {
    326                 assert( addrExpr->result );
    327                 addrExpr->set_arg( doSpecialization( addrExpr->result, addrExpr->arg ) );
    328                 return addrExpr;
    329         }
    330 
    331327        Expression * Specialize::postmutate( CastExpr *castExpr ) {
    332328                if ( castExpr->result->isVoid() ) {
     
    334330                        return castExpr;
    335331                }
    336                 Expression *specialized = doSpecialization( castExpr->result, castExpr->arg );
     332                Expression *specialized = doSpecialization( castExpr->result, castExpr->arg, &castExpr->inferParams );
    337333                if ( specialized != castExpr->arg ) {
    338334                        // assume here that the specialization incorporates the cast
Note: See TracChangeset for help on using the changeset viewer.