Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/GenPoly/Box.cc

    r474a170 r271a5d3  
    3737#include "InitTweak/InitTweak.h"         // for getFunctionName, isAssignment
    3838#include "Lvalue.h"                      // for generalizedLvalue
     39#include "ResolvExpr/TypeEnvironment.h"  // for EqvClass
    3940#include "ResolvExpr/typeops.h"          // for typesCompatible
    4041#include "ScopedSet.h"                   // for ScopedSet, ScopedSet<>::iter...
     
    9495                  private:
    9596                        /// Pass the extra type parameters from polymorphic generic arguments or return types into a function application
    96                         /// Will insert 0, 2 or 3 more arguments.
    97                         std::list< Expression *>::iterator passArgTypeVars( ApplicationExpr *appExpr, Type *parmType, Type *argBaseType, std::list< Expression *>::iterator arg, const TyVarMap &exprTyVars, std::set< std::string > &seenTypes );
     97                        void passArgTypeVars( ApplicationExpr *appExpr, Type *parmType, Type *argBaseType, std::list< Expression *>::iterator &arg, const TyVarMap &exprTyVars, std::set< std::string > &seenTypes );
    9898                        /// passes extra type parameters into a polymorphic function application
    9999                        /// Returns an iterator to the first argument after the added
     
    488488                                makeTyVarMap( functionType, scopeTyVars );
    489489
     490                                std::list< DeclarationWithType *> &paramList = functionType->parameters;
    490491                                std::list< FunctionType const *> functions;
    491492                                for ( TypeDecl * const tyVar : functionType->forall ) {
     
    494495                                        } // for
    495496                                } // for
    496                                 for ( DeclarationWithType * const arg : functionType->parameters ) {
     497                                for ( DeclarationWithType * const arg : paramList ) {
    497498                                        findFunction( arg->get_type(), functions, scopeTyVars, needsAdapter );
    498499                                } // for
     
    531532                }
    532533
    533                 std::list< Expression *>::iterator Pass1::passArgTypeVars( ApplicationExpr *appExpr, Type *parmType, Type *argBaseType, std::list< Expression *>::iterator arg, const TyVarMap &exprTyVars, std::set< std::string > &seenTypes ) {
     534                void Pass1::passArgTypeVars( ApplicationExpr *appExpr, Type *parmType, Type *argBaseType, std::list< Expression *>::iterator &arg, const TyVarMap &exprTyVars, std::set< std::string > &seenTypes ) {
    534535                        Type *polyType = isPolyType( parmType, exprTyVars );
    535536                        if ( polyType && ! dynamic_cast< TypeInstType* >( polyType ) ) {
    536537                                std::string typeName = mangleType( polyType );
    537                                 if ( seenTypes.count( typeName ) ) return arg;
     538                                if ( seenTypes.count( typeName ) ) return;
    538539
    539540                                arg = appExpr->get_args().insert( arg, new SizeofExpr( argBaseType->clone() ) );
     
    555556                                seenTypes.insert( typeName );
    556557                        }
    557                         return arg;
    558558                }
    559559
     
    562562                        std::list< Expression *>::iterator arg = appExpr->args.begin();
    563563                        // pass size/align for type variables
    564                         // NOTE: This is iterating over a map. This means the sorting
    565                         // order of the keys changes behaviour, as the iteration order
    566                         // is visible outside the loop. - The order matches the orignal
    567                         // order because the vars have been renamed with numbers that,
    568                         // even when converted to strings, sort in the original order.
    569                         // (At least, that is the best explination I have.)
    570564                        for ( std::pair<std::string, TypeDecl::Data> const & tyParam : exprTyVars ) {
    571                                 if ( !tyParam.second.isComplete ) continue;
    572                                 Type *concrete = env->lookup( tyParam.first );
    573                                 // If there is an unbound type variable, it should have detected already.
    574                                 assertf( concrete, "Unbound type variable: %s in: %s",
    575                                         toCString( tyParam.first ), toCString( *env ) );
    576 
    577                                 arg = appExpr->get_args().insert( arg, new SizeofExpr( concrete->clone() ) );
    578                                 arg++;
    579                                 arg = appExpr->get_args().insert( arg, new AlignofExpr( concrete->clone() ) );
    580                                 arg++;
     565                                ResolvExpr::EqvClass eqvClass;
     566                                if ( tyParam.second.isComplete ) {
     567                                        Type *concrete = env->lookup( tyParam.first );
     568                                        // If there is an unbound type variable, it should have detected already.
     569                                        assertf( concrete, "Unbound type variable: %s in: %s",
     570                                                toCString( tyParam.first ), toCString( *env ) );
     571
     572                                        arg = appExpr->get_args().insert( arg, new SizeofExpr( concrete->clone() ) );
     573                                        arg++;
     574                                        arg = appExpr->get_args().insert( arg, new AlignofExpr( concrete->clone() ) );
     575                                        arg++;
     576                                } // if
    581577                        } // for
    582578
     
    586582                        assert( funcType );
    587583
    588                         // Iterator over the original function arguments.
    589                         std::list< Expression* >::const_iterator fnArg;
    590                         // Names for generic types we've seen.
    591                         std::set< std::string > seenTypes;
     584                        // These iterators don't advance in unison.
     585                        std::list< DeclarationWithType* >::const_iterator fnParm = funcType->get_parameters().begin();
     586                        std::list< Expression* >::const_iterator fnArg = arg;
     587                        std::set< std::string > seenTypes; ///< names for generic types we've seen
    592588
    593589                        // a polymorphic return type may need to be added to the argument list
    594590                        if ( polyRetType ) {
    595591                                Type *concRetType = replaceWithConcrete( polyRetType, env );
    596                                 arg = passArgTypeVars( appExpr, polyRetType, concRetType, arg, exprTyVars, seenTypes );
    597                                 // Skip the return parameter in the argument list.
    598                                 fnArg = arg + 1;
    599                         } else {
    600                                 fnArg = arg;
     592                                passArgTypeVars( appExpr, polyRetType, concRetType, arg, exprTyVars, seenTypes );
     593                                ++fnArg; // skip the return parameter in the argument list
    601594                        }
    602595
    603596                        // add type information args for presently unseen types in parameter list
    604                         std::list< DeclarationWithType* >::const_iterator fnParm = funcType->get_parameters().begin();
    605597                        for ( ; fnParm != funcType->get_parameters().end() && fnArg != appExpr->get_args().end(); ++fnParm, ++fnArg ) {
     598                                if ( ! (*fnArg)->get_result() ) continue;
    606599                                Type * argType = (*fnArg)->get_result();
    607                                 if ( ! argType ) continue;
    608                                 arg = passArgTypeVars( appExpr, (*fnParm)->get_type(), argType, arg, exprTyVars, seenTypes );
     600                                passArgTypeVars( appExpr, (*fnParm)->get_type(), argType, arg, exprTyVars, seenTypes );
    609601                        }
    610602                        return arg;
     
    688680                Expression *Pass1::applyAdapter( ApplicationExpr *appExpr, FunctionType *function ) {
    689681                        Expression *ret = appExpr;
     682//                      if ( ! function->get_returnVals().empty() && isPolyType( function->get_returnVals().front()->get_type(), tyVars ) ) {
    690683                        if ( isDynRet( function, scopeTyVars ) ) {
    691684                                ret = addRetParam( appExpr, function->returnVals.front()->get_type() );
     
    779772
    780773                void Pass1::addInferredParams( ApplicationExpr *appExpr, std::list< Expression *>::iterator arg, FunctionType *functionType, const TyVarMap &tyVars ) {
     774                        std::list< Expression *>::iterator cur = arg;
    781775                        for ( TypeDecl * const tyVar : functionType->forall ) {
    782776                                for ( DeclarationWithType * const assert : tyVar->assertions ) {
     
    785779                                        Expression *newExpr = inferParam->second.expr->clone();
    786780                                        boxParam( newExpr, assert->get_type(), tyVars );
    787                                         arg = appExpr->get_args().insert( arg, newExpr );
    788                                         ++arg;
     781                                        appExpr->get_args().insert( cur, newExpr );
    789782                                } // for
    790783                        } // for
     
    929922                                // only attempt to create an adapter or pass one as a parameter if we haven't already done so for this
    930923                                // pre-substitution parameter function type.
    931                                 // The second part of the insert result is "is the value new".
    932                                 if ( adaptersDone.insert( mangleName ).second ) {
     924                                if ( adaptersDone.find( mangleName ) == adaptersDone.end() ) {
     925                                        adaptersDone.insert( adaptersDone.begin(), mangleName );
    933926
    934927                                        // apply substitution to type variables to figure out what the adapter's type should look like
     
    11131106
    11141107                        Expression *ret = appExpr;
    1115                         // Save iterator to the first original parameter (works with lists).
    11161108                        std::list< Expression *>::iterator paramBegin = appExpr->get_args().begin();
    11171109
     
    11801172
    11811173                void Pass1::premutate( AddressExpr * ) { visit_children = false; }
    1182 
    11831174                Expression * Pass1::postmutate( AddressExpr * addrExpr ) {
    11841175                        assert( addrExpr->arg->result && ! addrExpr->arg->result->isVoid() );
     
    12411232
    12421233                void Pass2::addAdapters( FunctionType *functionType ) {
     1234                        std::list< DeclarationWithType *> &paramList = functionType->parameters;
    12431235                        std::list< FunctionType const *> functions;
    12441236                        for ( DeclarationWithType * const arg : functionType->parameters ) {
     
    12531245                                        std::string adapterName = makeAdapterName( mangleName );
    12541246                                        // adapter may not be used in body, pass along with unused attribute.
    1255                                         functionType->parameters.push_front(
    1256                                                 new ObjectDecl( adapterName, Type::StorageClasses(), LinkageSpec::C, 0, new PointerType( Type::Qualifiers(), makeAdapterType( funType, scopeTyVars ) ), 0, { new Attribute( "unused" ) } ) );
     1247                                        paramList.push_front( new ObjectDecl( adapterName, Type::StorageClasses(), LinkageSpec::C, 0, new PointerType( Type::Qualifiers(), makeAdapterType( funType, scopeTyVars ) ), 0, { new Attribute( "unused" ) } ) );
    12571248                                        adaptersDone.insert( adaptersDone.begin(), mangleName );
    12581249                                }
    12591250                        }
     1251//  deleteAll( functions );
    12601252                }
    12611253
Note: See TracChangeset for help on using the changeset viewer.