Changeset 9f70a67b


Ignore:
Timestamp:
Nov 29, 2022, 11:49:44 AM (16 months ago)
Author:
Andrew Beach <ajbeach@…>
Branches:
ADT, ast-experimental, master
Children:
5a4b403
Parents:
dacd8e6e
Message:

Cleaning old box pass for easier translation. Again, this loop confuses me. Some other general clean-up.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/GenPoly/Box.cc

    rdacd8e6e r9f70a67b  
    3737#include "InitTweak/InitTweak.h"         // for getFunctionName, isAssignment
    3838#include "Lvalue.h"                      // for generalizedLvalue
    39 #include "ResolvExpr/TypeEnvironment.h"  // for EqvClass
    4039#include "ResolvExpr/typeops.h"          // for typesCompatible
    4140#include "ScopedSet.h"                   // for ScopedSet, ScopedSet<>::iter...
     
    9594                  private:
    9695                        /// 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.
    9797                        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
     
    488488                                makeTyVarMap( functionType, scopeTyVars );
    489489
    490                                 std::list< DeclarationWithType *> &paramList = functionType->parameters;
    491490                                std::list< FunctionType const *> functions;
    492491                                for ( TypeDecl * const tyVar : functionType->forall ) {
     
    495494                                        } // for
    496495                                } // for
    497                                 for ( DeclarationWithType * const arg : paramList ) {
     496                                for ( DeclarationWithType * const arg : functionType->parameters ) {
    498497                                        findFunction( arg->get_type(), functions, scopeTyVars, needsAdapter );
    499498                                } // for
     
    562561                        std::list< Expression *>::iterator arg = appExpr->args.begin();
    563562                        // pass size/align for type variables
     563                        // NOTE: This is iterating over a map. This means the sorting
     564                        // order of the keys changes behaviour, as the iteration order
     565                        // is visible outside the loop.
     566                        // TODO: I cannot figure out how this gets matched in the later
     567                        // passes the modify the function.
    564568                        for ( std::pair<std::string, TypeDecl::Data> const & tyParam : exprTyVars ) {
    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
     569                                if ( !tyParam.second.isComplete ) continue;
     570                                Type *concrete = env->lookup( tyParam.first );
     571                                // If there is an unbound type variable, it should have detected already.
     572                                assertf( concrete, "Unbound type variable: %s in: %s",
     573                                        toCString( tyParam.first ), toCString( *env ) );
     574
     575                                arg = appExpr->get_args().insert( arg, new SizeofExpr( concrete->clone() ) );
     576                                arg++;
     577                                arg = appExpr->get_args().insert( arg, new AlignofExpr( concrete->clone() ) );
     578                                arg++;
    577579                        } // for
    578580
     
    582584                        assert( funcType );
    583585
    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
     586                        // Iterator over the original function arguments.
     587                        std::list< Expression* >::const_iterator fnArg;
     588                        // Names for generic types we've seen.
     589                        std::set< std::string > seenTypes;
    588590
    589591                        // a polymorphic return type may need to be added to the argument list
     
    591593                                Type *concRetType = replaceWithConcrete( polyRetType, env );
    592594                                passArgTypeVars( appExpr, polyRetType, concRetType, arg, exprTyVars, seenTypes );
    593                                 ++fnArg; // skip the return parameter in the argument list
     595                                // Skip the return parameter in the argument list.
     596                                fnArg = arg + 1;
     597                        } else {
     598                                fnArg = arg;
    594599                        }
    595600
    596601                        // add type information args for presently unseen types in parameter list
     602                        std::list< DeclarationWithType* >::const_iterator fnParm = funcType->get_parameters().begin();
    597603                        for ( ; fnParm != funcType->get_parameters().end() && fnArg != appExpr->get_args().end(); ++fnParm, ++fnArg ) {
    598                                 if ( ! (*fnArg)->get_result() ) continue;
    599604                                Type * argType = (*fnArg)->get_result();
     605                                if ( ! argType ) continue;
    600606                                passArgTypeVars( appExpr, (*fnParm)->get_type(), argType, arg, exprTyVars, seenTypes );
    601607                        }
     
    680686                Expression *Pass1::applyAdapter( ApplicationExpr *appExpr, FunctionType *function ) {
    681687                        Expression *ret = appExpr;
    682 //                      if ( ! function->get_returnVals().empty() && isPolyType( function->get_returnVals().front()->get_type(), tyVars ) ) {
    683688                        if ( isDynRet( function, scopeTyVars ) ) {
    684689                                ret = addRetParam( appExpr, function->returnVals.front()->get_type() );
     
    772777
    773778                void Pass1::addInferredParams( ApplicationExpr *appExpr, std::list< Expression *>::iterator arg, FunctionType *functionType, const TyVarMap &tyVars ) {
    774                         std::list< Expression *>::iterator cur = arg;
    775779                        for ( TypeDecl * const tyVar : functionType->forall ) {
    776780                                for ( DeclarationWithType * const assert : tyVar->assertions ) {
     
    779783                                        Expression *newExpr = inferParam->second.expr->clone();
    780784                                        boxParam( newExpr, assert->get_type(), tyVars );
    781                                         appExpr->get_args().insert( cur, newExpr );
     785                                        arg = appExpr->get_args().insert( arg, newExpr );
     786                                        ++arg;
    782787                                } // for
    783788                        } // for
     
    922927                                // only attempt to create an adapter or pass one as a parameter if we haven't already done so for this
    923928                                // pre-substitution parameter function type.
    924                                 if ( adaptersDone.find( mangleName ) == adaptersDone.end() ) {
    925                                         adaptersDone.insert( adaptersDone.begin(), mangleName );
     929                                // The second part of the insert result is "is the value new".
     930                                if ( adaptersDone.insert( mangleName ).second ) {
    926931
    927932                                        // apply substitution to type variables to figure out what the adapter's type should look like
     
    11061111
    11071112                        Expression *ret = appExpr;
     1113                        // Save iterator to the first original parameter (works with lists).
    11081114                        std::list< Expression *>::iterator paramBegin = appExpr->get_args().begin();
    11091115
     
    11721178
    11731179                void Pass1::premutate( AddressExpr * ) { visit_children = false; }
     1180
    11741181                Expression * Pass1::postmutate( AddressExpr * addrExpr ) {
    11751182                        assert( addrExpr->arg->result && ! addrExpr->arg->result->isVoid() );
     
    12321239
    12331240                void Pass2::addAdapters( FunctionType *functionType ) {
    1234                         std::list< DeclarationWithType *> &paramList = functionType->parameters;
    12351241                        std::list< FunctionType const *> functions;
    12361242                        for ( DeclarationWithType * const arg : functionType->parameters ) {
     
    12451251                                        std::string adapterName = makeAdapterName( mangleName );
    12461252                                        // adapter may not be used in body, pass along with unused attribute.
    1247                                         paramList.push_front( new ObjectDecl( adapterName, Type::StorageClasses(), LinkageSpec::C, 0, new PointerType( Type::Qualifiers(), makeAdapterType( funType, scopeTyVars ) ), 0, { new Attribute( "unused" ) } ) );
     1253                                        functionType->parameters.push_front(
     1254                                                new ObjectDecl( adapterName, Type::StorageClasses(), LinkageSpec::C, 0, new PointerType( Type::Qualifiers(), makeAdapterType( funType, scopeTyVars ) ), 0, { new Attribute( "unused" ) } ) );
    12481255                                        adaptersDone.insert( adaptersDone.begin(), mangleName );
    12491256                                }
    12501257                        }
    1251 //  deleteAll( functions );
    12521258                }
    12531259
     
    13221328                        ObjectDecl newPtr( "", Type::StorageClasses(), LinkageSpec::C, 0,
    13231329                                           new PointerType( Type::Qualifiers(), new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ) ), 0 );
     1330                        // NOTE: This loop somehow consistently matching one in
     1331                        // `passTypeVars` even though it does loop in the same way.
    13241332                        for ( TypeDecl * const tyParam : funcType->get_forall() ) {
    13251333                                ObjectDecl *sizeParm, *alignParm;
Note: See TracChangeset for help on using the changeset viewer.