Changeset 201182a for src


Ignore:
Timestamp:
Sep 25, 2017, 7:10:17 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:
a31b384
Parents:
fc72845d
Message:

Convert Box Pass1 to PassVisitor?

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/GenPoly/Box.cc

    rfc72845d r201182a  
    7777
    7878                /// Replaces polymorphic return types with out-parameters, replaces calls to polymorphic functions with adapter calls as needed, and adds appropriate type variables to the function call
    79                 class Pass1 final : public PolyMutator {
     79                class Pass1 final : public BoxPass, public WithTypeSubstitution, public WithStmtsToAdd, public WithGuards, public WithVisitorRef<Pass1>, public WithShortCircuiting {
    8080                  public:
    8181                        Pass1();
    8282
    83                         using PolyMutator::mutate;
    84                         virtual Expression *mutate( ApplicationExpr *appExpr ) override;
    85                         virtual Expression *mutate( AddressExpr *addrExpr ) override;
    86                         virtual Expression *mutate( UntypedExpr *expr ) override;
    87                         virtual DeclarationWithType* mutate( FunctionDecl *functionDecl ) override;
    88                         virtual TypeDecl *mutate( TypeDecl *typeDecl ) override;
    89                         virtual Expression *mutate( CommaExpr *commaExpr ) override;
    90                         virtual Expression *mutate( ConditionalExpr *condExpr ) override;
    91                         virtual Statement * mutate( ReturnStmt *returnStmt ) override;
    92                         virtual Type *mutate( PointerType *pointerType ) override;
    93                         virtual Type * mutate( FunctionType *functionType ) override;
    94 
    95                         virtual void doBeginScope() override;
    96                         virtual void doEndScope() override;
     83                        void premutate( FunctionDecl * functionDecl );
     84                        void premutate( TypeDecl * typeDecl );
     85                        void premutate( CommaExpr * commaExpr );
     86                        Expression * postmutate( ApplicationExpr * appExpr );
     87                        Expression * postmutate( UntypedExpr *expr );
     88                        void premutate( AddressExpr * addrExpr );
     89                        Expression * postmutate( AddressExpr * addrExpr );
     90                        void premutate( ReturnStmt * returnStmt );
     91                        void premutate( PointerType * pointerType );
     92                        void premutate( FunctionType * functionType );
     93
     94                        void beginScope();
     95                        void endScope();
    9796                  private:
    9897                        /// Pass the extra type parameters from polymorphic generic arguments or return types into a function application
     
    160159                /// * Calculates polymorphic offsetof expressions from offset array
    161160                /// * Inserts dynamic calculation of polymorphic type layouts where needed
    162                 class PolyGenericCalculator final : public WithGuards, public WithVisitorRef<PolyGenericCalculator>, public WithStmtsToAdd, public WithDeclsToAdd, public WithTypeSubstitution {
     161                class PolyGenericCalculator final : public BoxPass, public WithGuards, public WithVisitorRef<PolyGenericCalculator>, public WithStmtsToAdd, public WithDeclsToAdd, public WithTypeSubstitution {
    163162                public:
    164163                        PolyGenericCalculator();
     
    199198                        ScopedSet< std::string > knownOffsets;          ///< Set of non-generic types for which the offset array exists in the current scope, indexed by offsetofName
    200199                        UniqueName bufNamer;                           ///< Namer for VLA buffers
    201                         TyVarMap scopeTyVars;
    202200                };
    203201
     
    247245        void box( std::list< Declaration *>& translationUnit ) {
    248246                PassVisitor<LayoutFunctionBuilder> layoutBuilder;
    249                 Pass1 pass1;
     247                PassVisitor<Pass1> pass1;
    250248                Pass2 pass2;
    251249                PassVisitor<PolyGenericCalculator> polyCalculator;
     
    253251
    254252                acceptAll( translationUnit, layoutBuilder );
    255                 mutateTranslationUnit/*All*/( translationUnit, pass1 );
     253                mutateAll( translationUnit, pass1 );
    256254                mutateTranslationUnit/*All*/( translationUnit, pass2 );
    257255                mutateAll( translationUnit, polyCalculator );
     
    500498                Pass1::Pass1() : tempNamer( "_temp" ) {}
    501499
    502                 DeclarationWithType *Pass1::mutate( FunctionDecl *functionDecl ) {
     500                void Pass1::premutate( FunctionDecl *functionDecl ) {
    503501                        if ( functionDecl->get_statements() ) {         // empty routine body ?
    504502                                // std::cerr << "mutating function: " << functionDecl->get_mangleName() << std::endl;
    505                                 doBeginScope();
    506                                 scopeTyVars.beginScope();
    507 
    508                                 DeclarationWithType *oldRetval = retval;
     503                                GuardScope( scopeTyVars );
     504                                GuardValue( retval );
    509505
    510506                                // process polymorphic return value
    511507                                retval = nullptr;
    512                                 if ( isDynRet( functionDecl->get_functionType() ) && functionDecl->get_linkage() != LinkageSpec::C ) {
    513                                         retval = functionDecl->get_functionType()->get_returnVals().front();
     508                                FunctionType *functionType = functionDecl->type;
     509                                if ( isDynRet( functionType ) && functionDecl->linkage != LinkageSpec::C ) {
     510                                        retval = functionType->returnVals.front();
    514511
    515512                                        // give names to unnamed return values
    516                                         if ( retval->get_name() == "" ) {
    517                                                 retval->set_name( "_retparm" );
    518                                                 retval->set_linkage( LinkageSpec::C );
     513                                        if ( retval->name == "" ) {
     514                                                retval->name = "_retparm";
     515                                                retval->linkage = LinkageSpec::C;
    519516                                        } // if
    520517                                } // if
    521518
    522                                 FunctionType *functionType = functionDecl->get_functionType();
    523                                 makeTyVarMap( functionDecl->get_functionType(), scopeTyVars );
    524 
    525                                 std::list< DeclarationWithType *> &paramList = functionType->get_parameters();
     519                                makeTyVarMap( functionType, scopeTyVars );
     520
     521                                std::list< DeclarationWithType *> &paramList = functionType->parameters;
    526522                                std::list< FunctionType *> functions;
    527                                 for ( Type::ForallList::iterator tyVar = functionType->get_forall().begin(); tyVar != functionType->get_forall().end(); ++tyVar ) {
    528                                         for ( std::list< DeclarationWithType *>::iterator assert = (*tyVar)->get_assertions().begin(); assert != (*tyVar)->get_assertions().end(); ++assert ) {
     523                                for ( Type::ForallList::iterator tyVar = functionType->forall.begin(); tyVar != functionType->forall.end(); ++tyVar ) {
     524                                        for ( std::list< DeclarationWithType *>::iterator assert = (*tyVar)->assertions.begin(); assert != (*tyVar)->assertions.end(); ++assert ) {
    529525                                                findFunction( (*assert)->get_type(), functions, scopeTyVars, needsAdapter );
    530526                                        } // for
     
    541537                                        } // if
    542538                                } // for
    543 
    544                                 functionDecl->set_statements( functionDecl->get_statements()->acceptMutator( *this ) );
    545 
    546                                 scopeTyVars.endScope();
    547                                 retval = oldRetval;
    548                                 doEndScope();
    549539                                // std::cerr << "end function: " << functionDecl->get_mangleName() << std::endl;
    550540                        } // if
    551                         return functionDecl;
    552                 }
    553 
    554                 TypeDecl *Pass1::mutate( TypeDecl *typeDecl ) {
     541                }
     542
     543                void Pass1::premutate( TypeDecl *typeDecl ) {
    555544                        addToTyVarMap( typeDecl, scopeTyVars );
    556                         return dynamic_cast<TypeDecl*>( Mutator::mutate( typeDecl ) );
    557                 }
    558 
    559                 Expression *Pass1::mutate( CommaExpr *commaExpr ) {
     545                }
     546
     547                void Pass1::premutate( CommaExpr *commaExpr ) {
    560548                        // Attempting to find application expressions that were mutated by the copy constructor passes
    561549                        // to use an explicit return variable, so that the variable can be reused as a parameter to the
     
    573561                                }
    574562                        }
    575 
    576                         commaExpr->set_arg1( maybeMutate( commaExpr->get_arg1(), *this ) );
    577                         commaExpr->set_arg2( maybeMutate( commaExpr->get_arg2(), *this ) );
    578                         return commaExpr;
    579                 }
    580 
    581                 Expression *Pass1::mutate( ConditionalExpr *condExpr ) {
    582                         condExpr->set_arg1( maybeMutate( condExpr->get_arg1(), *this ) );
    583                         condExpr->set_arg2( maybeMutate( condExpr->get_arg2(), *this ) );
    584                         condExpr->set_arg3( maybeMutate( condExpr->get_arg3(), *this ) );
    585                         return condExpr;
    586 
    587563                }
    588564
     
    658634                ObjectDecl *Pass1::makeTemporary( Type *type ) {
    659635                        ObjectDecl *newObj = new ObjectDecl( tempNamer.newName(), Type::StorageClasses(), LinkageSpec::C, 0, type, 0 );
    660                         stmtsToAdd.push_back( new DeclStmt( noLabels, newObj ) );
     636                        stmtsToAddBefore.push_back( new DeclStmt( noLabels, newObj ) );
    661637                        return newObj;
    662638                }
     
    774750                                        ObjectDecl *newObj = new ObjectDecl( tempNamer.newName(), Type::StorageClasses(), LinkageSpec::C, 0, newType, 0 );
    775751                                        newObj->get_type()->get_qualifiers() = Type::Qualifiers(); // TODO: is this right???
    776                                         stmtsToAdd.push_back( new DeclStmt( noLabels, newObj ) );
     752                                        stmtsToAddBefore.push_back( new DeclStmt( noLabels, newObj ) );
    777753                                        UntypedExpr *assign = new UntypedExpr( new NameExpr( "?=?" ) ); // TODO: why doesn't this just use initialization syntax?
    778754                                        assign->get_args().push_back( new VariableExpr( newObj ) );
    779755                                        assign->get_args().push_back( arg );
    780                                         stmtsToAdd.push_back( new ExprStmt( noLabels, assign ) );
     756                                        stmtsToAddBefore.push_back( new ExprStmt( noLabels, assign ) );
    781757                                        arg = new AddressExpr( new VariableExpr( newObj ) );
    782758                                } // if
     
    960936                                                std::pair< AdapterIter, bool > answer = adapters.insert( std::pair< std::string, DeclarationWithType *>( mangleName, newAdapter ) );
    961937                                                adapter = answer.first;
    962                                                 stmtsToAdd.push_back( new DeclStmt( noLabels, newAdapter ) );
     938                                                stmtsToAddBefore.push_back( new DeclStmt( noLabels, newAdapter ) );
    963939                                        } // if
    964940                                        assert( adapter != adapters.end() );
     
    11171093                }
    11181094
    1119                 Expression *Pass1::mutate( ApplicationExpr *appExpr ) {
     1095                Expression *Pass1::postmutate( ApplicationExpr *appExpr ) {
    11201096                        // std::cerr << "mutate appExpr: " << InitTweak::getFunctionName( appExpr ) << std::endl;
    11211097                        // for ( TyVarMap::iterator i = scopeTyVars.begin(); i != scopeTyVars.end(); ++i ) {
     
    11231099                        // }
    11241100                        // std::cerr << "\n";
    1125                         appExpr->get_function()->acceptMutator( *this );
    1126                         mutateAll( appExpr->get_args(), *this );
    1127 
    1128                         assert( appExpr->get_function()->has_result() );
    1129                         FunctionType * function = getFunctionType( appExpr->get_function()->get_result() );
    1130                         assertf( function, "ApplicationExpr has non-function type: %s", toString( appExpr->get_function()->get_result() ).c_str() );
     1101
     1102                        assert( appExpr->function->result );
     1103                        FunctionType * function = getFunctionType( appExpr->function->result );
     1104                        assertf( function, "ApplicationExpr has non-function type: %s", toString( appExpr->function->result ).c_str() );
    11311105
    11321106                        if ( Expression *newExpr = handleIntrinsics( appExpr ) ) {
     
    11811155                }
    11821156
    1183                 Expression *Pass1::mutate( UntypedExpr *expr ) {
    1184                         if ( expr->has_result() && isPolyType( expr->get_result(), scopeTyVars, env ) ) {
    1185                                 if ( NameExpr *name = dynamic_cast< NameExpr *>( expr->get_function() ) ) {
     1157                Expression * Pass1::postmutate( UntypedExpr *expr ) {
     1158                        if ( expr->result && isPolyType( expr->result, scopeTyVars, env ) ) {
     1159                                if ( NameExpr *name = dynamic_cast< NameExpr *>( expr->function ) ) {
    11861160                                        if ( name->get_name() == "*?" ) {
    1187                                                 Expression *ret = expr->get_args().front();
    1188                                                 expr->get_args().clear();
     1161                                                Expression *ret = expr->args.front();
     1162                                                expr->args.clear();
    11891163                                                delete expr;
    1190                                                 return ret->acceptMutator( *this );
     1164                                                return ret;
    11911165                                        } // if
    11921166                                } // if
    11931167                        } // if
    1194                         return PolyMutator::mutate( expr );
    1195                 }
    1196 
    1197                 Expression *Pass1::mutate( AddressExpr *addrExpr ) {
     1168                        return expr;
     1169                }
     1170
     1171                void Pass1::premutate( AddressExpr * ) { visit_children = false; }
     1172                Expression * Pass1::postmutate( AddressExpr * addrExpr ) {
    11981173                        assert( addrExpr->get_arg()->has_result() && ! addrExpr->get_arg()->get_result()->isVoid() );
    11991174
     
    12151190                        // isPolyType check needs to happen before mutating addrExpr arg, so pull it forward
    12161191                        // out of the if condition.
    1217                         addrExpr->set_arg( mutateExpression( addrExpr->get_arg() ) );
     1192                        addrExpr->arg = addrExpr->get_arg()->acceptMutator( *visitor );
    12181193                        // ... but must happen after mutate, since argument might change (e.g. intrinsic *?, ?[?]) - re-evaluate above comment
    12191194                        bool polytype = isPolyType( addrExpr->get_arg()->get_result(), scopeTyVars, env );
     
    12301205                }
    12311206
    1232                 Statement * Pass1::mutate( ReturnStmt *returnStmt ) {
    1233                         if ( retval && returnStmt->get_expr() ) {
    1234                                 assert( returnStmt->get_expr()->has_result() && ! returnStmt->get_expr()->get_result()->isVoid() );
    1235                                 delete returnStmt->get_expr();
    1236                                 returnStmt->set_expr( 0 );
    1237                         } else {
    1238                                 returnStmt->set_expr( mutateExpression( returnStmt->get_expr() ) );
     1207                void Pass1::premutate( ReturnStmt *returnStmt ) {
     1208                        if ( retval && returnStmt->expr ) {
     1209                                assert( returnStmt->expr->result && ! returnStmt->expr->result->isVoid() );
     1210                                delete returnStmt->expr;
     1211                                returnStmt->expr = nullptr;
    12391212                        } // if
    1240                         return returnStmt;
    1241                 }
    1242 
    1243                 Type * Pass1::mutate( PointerType *pointerType ) {
    1244                         scopeTyVars.beginScope();
     1213                }
     1214
     1215                void Pass1::premutate( PointerType *pointerType ) {
     1216                        GuardScope( scopeTyVars );
    12451217                        makeTyVarMap( pointerType, scopeTyVars );
    1246 
    1247                         Type *ret = Mutator::mutate( pointerType );
    1248 
    1249                         scopeTyVars.endScope();
    1250                         return ret;
    1251                 }
    1252 
    1253                 Type * Pass1::mutate( FunctionType *functionType ) {
    1254                         scopeTyVars.beginScope();
     1218                }
     1219
     1220                void Pass1::premutate( FunctionType *functionType ) {
     1221                        GuardScope( scopeTyVars );
    12551222                        makeTyVarMap( functionType, scopeTyVars );
    1256 
    1257                         Type *ret = Mutator::mutate( functionType );
    1258 
    1259                         scopeTyVars.endScope();
    1260                         return ret;
    1261                 }
    1262 
    1263                 void Pass1::doBeginScope() {
     1223                }
     1224
     1225                void Pass1::beginScope() {
    12641226                        adapters.beginScope();
    12651227                }
    12661228
    1267                 void Pass1::doEndScope() {
     1229                void Pass1::endScope() {
    12681230                        adapters.endScope();
    12691231                }
     
    14671429
    14681430                PolyGenericCalculator::PolyGenericCalculator()
    1469                         : knownLayouts(), knownOffsets(), bufNamer( "_buf" ), scopeTyVars( TypeDecl::Data{} ) {}
     1431                        : knownLayouts(), knownOffsets(), bufNamer( "_buf" ) {}
    14701432
    14711433                void PolyGenericCalculator::beginTypeScope( Type *ty ) {
Note: See TracChangeset for help on using the changeset viewer.