Changeset cf90b88 for src/GenPoly


Ignore:
Timestamp:
Sep 25, 2017, 6:15:03 PM (7 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:
fc72845d
Parents:
dc2334c
Message:

Convert Specialize to PassVisitor?

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/GenPoly/Specialize.cc

    rdc2334c rcf90b88  
    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
     
    4344
    4445namespace 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 );
     46        struct Specialize final : public WithTypeSubstitution, public WithStmtsToAdd, public WithVisitorRef<Specialize> {
     47                Expression * postmutate( ApplicationExpr *applicationExpr );
     48                Expression * postmutate( AddressExpr *castExpr );
     49                Expression * postmutate( CastExpr *castExpr );
    5450
    5551                void handleExplicitParams( ApplicationExpr *appExpr );
     
    204200        }
    205201
    206         struct EnvTrimmer : public Visitor {
     202        struct EnvTrimmer {
    207203                TypeSubstitution * env, * newEnv;
    208204                EnvTrimmer( TypeSubstitution * env, TypeSubstitution * newEnv ) : env( env ), newEnv( newEnv ){}
    209                 virtual void visit( TypeDecl * tyDecl ) {
     205                void previsit( TypeDecl * tyDecl ) {
    210206                        // transfer known bindings for seen type variables
    211                         if ( Type * t = env->lookup( tyDecl->get_name() ) ) {
    212                                 newEnv->add( tyDecl->get_name(), t );
     207                        if ( Type * t = env->lookup( tyDecl->name ) ) {
     208                                newEnv->add( tyDecl->name, t );
    213209                        }
    214210                }
     
    219215                if ( env ) {
    220216                        TypeSubstitution * newEnv = new TypeSubstitution();
    221                         EnvTrimmer trimmer( env, newEnv );
     217                        PassVisitor<EnvTrimmer> trimmer( env, newEnv );
    222218                        expr->accept( trimmer );
    223219                        return newEnv;
     
    277273                std::string oldParamPrefix = paramPrefix;
    278274                paramPrefix += "p";
    279                 // save stmtsToAdd in oldStmts
     275                // save stmtsToAddBefore in oldStmts
    280276                std::list< Statement* > oldStmts;
    281                 oldStmts.splice( oldStmts.end(), stmtsToAdd );
    282                 mutate( appExpr );
     277                oldStmts.splice( oldStmts.end(), stmtsToAddBefore );
     278                appExpr->acceptMutator( *visitor );
    283279                paramPrefix = oldParamPrefix;
    284280                // 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 );
     281                thunkFunc->statements->kids.splice( thunkFunc->statements->kids.end(), stmtsToAddBefore );
     282                // restore oldStmts into stmtsToAddBefore
     283                stmtsToAddBefore.splice( stmtsToAddBefore.end(), oldStmts );
    288284
    289285                // add return (or valueless expression) to the thunk
    290286                Statement *appStmt;
    291                 if ( funType->get_returnVals().empty() ) {
     287                if ( funType->returnVals.empty() ) {
    292288                        appStmt = new ExprStmt( noLabels, appExpr );
    293289                } else {
    294290                        appStmt = new ReturnStmt( noLabels, appExpr );
    295291                } // if
    296                 thunkFunc->get_statements()->get_kids().push_back( appStmt );
     292                thunkFunc->statements->kids.push_back( appStmt );
    297293
    298294                // add thunk definition to queue of statements to add
    299                 stmtsToAdd.push_back( new DeclStmt( noLabels, thunkFunc ) );
     295                stmtsToAddBefore.push_back( new DeclStmt( noLabels, thunkFunc ) );
    300296                // return address of thunk function as replacement expression
    301297                return new AddressExpr( new VariableExpr( thunkFunc ) );
     
    304300        void Specialize::handleExplicitParams( ApplicationExpr *appExpr ) {
    305301                // create thunks for the explicit parameters
    306                 assert( appExpr->get_function()->has_result() );
    307                 FunctionType *function = getFunctionType( appExpr->get_function()->get_result() );
     302                assert( appExpr->function->result );
     303                FunctionType *function = getFunctionType( appExpr->function->result );
    308304                assert( function );
    309305                std::list< DeclarationWithType* >::iterator formal;
    310306                std::list< Expression* >::iterator actual;
    311307                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 
     308                        *actual = doSpecialization( (*formal)->get_type(), *actual, &appExpr->get_inferParams() );
     309                }
     310        }
     311
     312        Expression * Specialize::postmutate( ApplicationExpr *appExpr ) {
    320313                if ( ! InitTweak::isIntrinsicCallExpr( appExpr ) ) {
    321314                        // create thunks for the inferred parameters
     
    331324        }
    332325
    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() ) );
     326        Expression * Specialize::postmutate( AddressExpr *addrExpr ) {
     327                assert( addrExpr->result );
     328                addrExpr->set_arg( doSpecialization( addrExpr->result, addrExpr->arg ) );
    337329                return addrExpr;
    338330        }
    339331
    340         Expression * Specialize::mutate( CastExpr *castExpr ) {
    341                 castExpr->get_arg()->acceptMutator( *this );
    342                 if ( castExpr->get_result()->isVoid() ) {
     332        Expression * Specialize::postmutate( CastExpr *castExpr ) {
     333                if ( castExpr->result->isVoid() ) {
    343334                        // can't specialize if we don't have a return value
    344335                        return castExpr;
    345336                }
    346                 Expression *specialized = doSpecialization( castExpr->get_result(), castExpr->get_arg() );
    347                 if ( specialized != castExpr->get_arg() ) {
     337                Expression *specialized = doSpecialization( castExpr->result, castExpr->arg );
     338                if ( specialized != castExpr->arg ) {
    348339                        // assume here that the specialization incorporates the cast
    349340                        return specialized;
     
    353344        }
    354345
    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 
    371346        void convertSpecializations( std::list< Declaration* >& translationUnit ) {
    372                 Specialize spec;
     347                PassVisitor<Specialize> spec;
    373348                mutateAll( translationUnit, spec );
    374349        }
Note: See TracChangeset for help on using the changeset viewer.