Ignore:
Timestamp:
Sep 26, 2017, 11:27:38 PM (7 years ago)
Author:
Peter A. Buhr <pabuhr@…>
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:
5dc26f5
Parents:
201aeb9
Message:

merge

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/GenPoly/Specialize.cc

    r201aeb9 rd67cdb7  
    2222#include <utility>                       // for pair
    2323
     24#include "Common/PassVisitor.h"
    2425#include "Common/SemanticError.h"        // for SemanticError
    2526#include "Common/UniqueName.h"           // for UniqueName
     
    2829#include "InitTweak/InitTweak.h"         // for isIntrinsicCallExpr
    2930#include "Parser/LinkageSpec.h"          // for C
    30 #include "PolyMutator.h"                 // for PolyMutator
    3131#include "ResolvExpr/FindOpenVars.h"     // for findOpenVars
    3232#include "ResolvExpr/TypeEnvironment.h"  // for OpenVarSet, AssertionSet
     
    4343
    4444namespace GenPoly {
    45         class Specialize final : public PolyMutator {
    46           public:
    47                 using PolyMutator::mutate;
    48                 virtual Expression * mutate( ApplicationExpr *applicationExpr ) override;
    49                 virtual Expression * mutate( AddressExpr *castExpr ) override;
    50                 virtual Expression * mutate( CastExpr *castExpr ) override;
    51                 // virtual Expression * mutate( LogicalExpr *logicalExpr );
    52                 // virtual Expression * mutate( ConditionalExpr *conditionalExpr );
    53                 // virtual Expression * mutate( CommaExpr *commaExpr );
     45        struct Specialize final : public WithTypeSubstitution, public WithStmtsToAdd, public WithVisitorRef<Specialize> {
     46                Expression * postmutate( ApplicationExpr *applicationExpr );
     47                Expression * postmutate( AddressExpr *castExpr );
     48                Expression * postmutate( CastExpr *castExpr );
    5449
    5550                void handleExplicitParams( ApplicationExpr *appExpr );
     
    204199        }
    205200
    206         struct EnvTrimmer : public Visitor {
     201        struct EnvTrimmer {
    207202                TypeSubstitution * env, * newEnv;
    208203                EnvTrimmer( TypeSubstitution * env, TypeSubstitution * newEnv ) : env( env ), newEnv( newEnv ){}
    209                 virtual void visit( TypeDecl * tyDecl ) {
     204                void previsit( TypeDecl * tyDecl ) {
    210205                        // transfer known bindings for seen type variables
    211                         if ( Type * t = env->lookup( tyDecl->get_name() ) ) {
    212                                 newEnv->add( tyDecl->get_name(), t );
     206                        if ( Type * t = env->lookup( tyDecl->name ) ) {
     207                                newEnv->add( tyDecl->name, t );
    213208                        }
    214209                }
     
    219214                if ( env ) {
    220215                        TypeSubstitution * newEnv = new TypeSubstitution();
    221                         EnvTrimmer trimmer( env, newEnv );
     216                        PassVisitor<EnvTrimmer> trimmer( env, newEnv );
    222217                        expr->accept( trimmer );
    223218                        return newEnv;
     
    277272                std::string oldParamPrefix = paramPrefix;
    278273                paramPrefix += "p";
    279                 // save stmtsToAdd in oldStmts
     274                // save stmtsToAddBefore in oldStmts
    280275                std::list< Statement* > oldStmts;
    281                 oldStmts.splice( oldStmts.end(), stmtsToAdd );
    282                 mutate( appExpr );
     276                oldStmts.splice( oldStmts.end(), stmtsToAddBefore );
     277                appExpr->acceptMutator( *visitor );
    283278                paramPrefix = oldParamPrefix;
    284279                // write any statements added for recursive specializations into the thunk body
    285                 thunkFunc->get_statements()->get_kids().splice( thunkFunc->get_statements()->get_kids().end(), stmtsToAdd );
    286                 // restore oldStmts into stmtsToAdd
    287                 stmtsToAdd.splice( stmtsToAdd.end(), oldStmts );
     280                thunkFunc->statements->kids.splice( thunkFunc->statements->kids.end(), stmtsToAddBefore );
     281                // restore oldStmts into stmtsToAddBefore
     282                stmtsToAddBefore.splice( stmtsToAddBefore.end(), oldStmts );
    288283
    289284                // add return (or valueless expression) to the thunk
    290285                Statement *appStmt;
    291                 if ( funType->get_returnVals().empty() ) {
     286                if ( funType->returnVals.empty() ) {
    292287                        appStmt = new ExprStmt( noLabels, appExpr );
    293288                } else {
    294289                        appStmt = new ReturnStmt( noLabels, appExpr );
    295290                } // if
    296                 thunkFunc->get_statements()->get_kids().push_back( appStmt );
     291                thunkFunc->statements->kids.push_back( appStmt );
    297292
    298293                // add thunk definition to queue of statements to add
    299                 stmtsToAdd.push_back( new DeclStmt( noLabels, thunkFunc ) );
     294                stmtsToAddBefore.push_back( new DeclStmt( noLabels, thunkFunc ) );
    300295                // return address of thunk function as replacement expression
    301296                return new AddressExpr( new VariableExpr( thunkFunc ) );
     
    304299        void Specialize::handleExplicitParams( ApplicationExpr *appExpr ) {
    305300                // create thunks for the explicit parameters
    306                 assert( appExpr->get_function()->has_result() );
    307                 FunctionType *function = getFunctionType( appExpr->get_function()->get_result() );
     301                assert( appExpr->function->result );
     302                FunctionType *function = getFunctionType( appExpr->function->result );
    308303                assert( function );
    309304                std::list< DeclarationWithType* >::iterator formal;
    310305                std::list< Expression* >::iterator actual;
    311306                for ( formal = function->get_parameters().begin(), actual = appExpr->get_args().begin(); formal != function->get_parameters().end() && actual != appExpr->get_args().end(); ++formal, ++actual ) {
    312                         *actual = doSpecialization( (*formal )->get_type(), *actual, &appExpr->get_inferParams() );
    313                 }
    314         }
    315 
    316         Expression * Specialize::mutate( ApplicationExpr *appExpr ) {
    317                 appExpr->get_function()->acceptMutator( *this );
    318                 mutateAll( appExpr->get_args(), *this );
    319 
     307                        *actual = doSpecialization( (*formal)->get_type(), *actual, &appExpr->get_inferParams() );
     308                }
     309        }
     310
     311        Expression * Specialize::postmutate( ApplicationExpr *appExpr ) {
    320312                if ( ! InitTweak::isIntrinsicCallExpr( appExpr ) ) {
    321313                        // create thunks for the inferred parameters
     
    331323        }
    332324
    333         Expression * Specialize::mutate( AddressExpr *addrExpr ) {
    334                 addrExpr->get_arg()->acceptMutator( *this );
    335                 assert( addrExpr->has_result() );
    336                 addrExpr->set_arg( doSpecialization( addrExpr->get_result(), addrExpr->get_arg() ) );
     325        Expression * Specialize::postmutate( AddressExpr *addrExpr ) {
     326                assert( addrExpr->result );
     327                addrExpr->set_arg( doSpecialization( addrExpr->result, addrExpr->arg ) );
    337328                return addrExpr;
    338329        }
    339330
    340         Expression * Specialize::mutate( CastExpr *castExpr ) {
    341                 castExpr->get_arg()->acceptMutator( *this );
    342                 if ( castExpr->get_result()->isVoid() ) {
     331        Expression * Specialize::postmutate( CastExpr *castExpr ) {
     332                if ( castExpr->result->isVoid() ) {
    343333                        // can't specialize if we don't have a return value
    344334                        return castExpr;
    345335                }
    346                 Expression *specialized = doSpecialization( castExpr->get_result(), castExpr->get_arg() );
    347                 if ( specialized != castExpr->get_arg() ) {
     336                Expression *specialized = doSpecialization( castExpr->result, castExpr->arg );
     337                if ( specialized != castExpr->arg ) {
    348338                        // assume here that the specialization incorporates the cast
    349339                        return specialized;
     
    353343        }
    354344
    355         // Removing these for now. Richard put these in for some reason, but it's not clear why.
    356         // In particular, copy constructors produce a comma expression, and with this code the parts
    357         // of that comma expression are not specialized, which causes problems.
    358 
    359         // Expression * Specialize::mutate( LogicalExpr *logicalExpr ) {
    360         //      return logicalExpr;
    361         // }
    362 
    363         // Expression * Specialize::mutate( ConditionalExpr *condExpr ) {
    364         //      return condExpr;
    365         // }
    366 
    367         // Expression * Specialize::mutate( CommaExpr *commaExpr ) {
    368         //      return commaExpr;
    369         // }
    370 
    371345        void convertSpecializations( std::list< Declaration* >& translationUnit ) {
    372                 Specialize spec;
     346                PassVisitor<Specialize> spec;
    373347                mutateAll( translationUnit, spec );
    374348        }
Note: See TracChangeset for help on using the changeset viewer.