Changeset 1132b62 for src/InitTweak


Ignore:
Timestamp:
Sep 20, 2016, 7:14:15 PM (9 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:
65660bd
Parents:
23b6643f
Message:

copy construct tuple function arguments, and destruct tuple function results

File:
1 edited

Legend:

Unmodified
Added
Removed
  • TabularUnified src/InitTweak/FixInit.cc

    r23b6643f r1132b62  
    7070                        /// create and resolve ctor/dtor expression: fname(var, [cpArg])
    7171                        ApplicationExpr * makeCtorDtor( const std::string & fname, ObjectDecl * var, Expression * cpArg = NULL );
     72                        ApplicationExpr * makeCtorDtor( const std::string & fname, Expression * thisArg, Expression * cpArg = NULL );
    7273                        /// true if type does not need to be copy constructed to ensure correctness
    73                         bool skipCopyConstruct( Type * );
     74                        bool skipCopyConstruct( Type * type );
     75                        void copyConstructArg( Expression *& arg, ImplicitCopyCtorExpr * impCpCtorExpr );
     76                        void destructRet( Expression * ret, ImplicitCopyCtorExpr * impCpCtorExpr );
    7477                private:
    7578                        TypeSubstitution * env;
     
    359362                ApplicationExpr * ResolveCopyCtors::makeCtorDtor( const std::string & fname, ObjectDecl * var, Expression * cpArg ) {
    360363                        assert( var );
     364                        return makeCtorDtor( fname, new AddressExpr( new VariableExpr( var ) ), cpArg );
     365                }
     366
     367                ApplicationExpr * ResolveCopyCtors::makeCtorDtor( const std::string & fname, Expression * thisArg, Expression * cpArg ) {
     368                        assert( thisArg );
    361369                        UntypedExpr * untyped = new UntypedExpr( new NameExpr( fname ) );
    362                         untyped->get_args().push_back( new AddressExpr( new VariableExpr( var ) ) );
     370                        untyped->get_args().push_back( thisArg );
    363371                        if (cpArg) untyped->get_args().push_back( cpArg->clone() );
    364372
     
    377385                }
    378386
     387                void ResolveCopyCtors::copyConstructArg( Expression *& arg, ImplicitCopyCtorExpr * impCpCtorExpr ) {
     388                        static UniqueName tempNamer("_tmp_cp");
     389                        CP_CTOR_PRINT( std::cerr << "Type Substitution: " << *impCpCtorExpr->get_env() << std::endl; )
     390                        assert( arg->has_result() );
     391                        Type * result = arg->get_result();
     392                        if ( skipCopyConstruct( result ) ) return; // skip certain non-copyable types
     393
     394                        if ( TupleExpr * tupleExpr = dynamic_cast< TupleExpr * >( arg ) ) {
     395                                for ( Expression * & expr : tupleExpr->get_exprs() ) {
     396                                        copyConstructArg( expr, impCpCtorExpr );
     397                                }
     398                                return;
     399                        }
     400
     401                        // type may involve type variables, so apply type substitution to get temporary variable's actual type
     402                        result = result->clone();
     403                        impCpCtorExpr->get_env()->apply( result );
     404                        ObjectDecl * tmp = new ObjectDecl( tempNamer.newName(), DeclarationNode::NoStorageClass, LinkageSpec::C, 0, result, 0 );
     405                        tmp->get_type()->set_isConst( false );
     406
     407                        // create and resolve copy constructor
     408                        CP_CTOR_PRINT( std::cerr << "makeCtorDtor for an argument" << std::endl; )
     409                        ApplicationExpr * cpCtor = makeCtorDtor( "?{}", tmp, arg );
     410
     411                        // if the chosen constructor is intrinsic, the copy is unnecessary, so
     412                        // don't create the temporary and don't call the copy constructor
     413                        VariableExpr * function = dynamic_cast< VariableExpr * >( cpCtor->get_function() );
     414                        assert( function );
     415                        if ( function->get_var()->get_linkage() != LinkageSpec::Intrinsic ) {
     416                                // replace argument to function call with temporary
     417                                arg = new CommaExpr( cpCtor, new VariableExpr( tmp ) );
     418                                impCpCtorExpr->get_tempDecls().push_back( tmp );
     419                                impCpCtorExpr->get_dtors().push_front( makeCtorDtor( "^?{}", tmp ) );
     420                        } // if
     421                }
     422
     423                void ResolveCopyCtors::destructRet( Expression * ret, ImplicitCopyCtorExpr * impCpCtorExpr ) {
     424                        if ( TupleType * tupleType = dynamic_cast< TupleType * > ( ret->get_result() ) ) {
     425                                int idx = 0;
     426                                for ( Type *& t : tupleType->get_types() ) {
     427                                        (void)t;
     428                                        destructRet( new TupleIndexExpr( ret->clone(), idx++ ), impCpCtorExpr );
     429                                }
     430                                return;
     431                        }
     432                        impCpCtorExpr->get_dtors().push_front( makeCtorDtor( "^?{}", new AddressExpr( ret ) ) );
     433                }
     434
    379435                void ResolveCopyCtors::visit( ImplicitCopyCtorExpr *impCpCtorExpr ) {
    380                         static UniqueName tempNamer("_tmp_cp");
    381                         static UniqueName retNamer("_tmp_cp_ret");
    382 
    383436                        CP_CTOR_PRINT( std::cerr << "ResolveCopyCtors: " << impCpCtorExpr << std::endl; )
    384437                        Visitor::visit( impCpCtorExpr );
     
    389442                        // take each argument and attempt to copy construct it.
    390443                        for ( Expression * & arg : appExpr->get_args() ) {
    391                                 CP_CTOR_PRINT( std::cerr << "Type Substitution: " << *impCpCtorExpr->get_env() << std::endl; )
    392                                 // xxx - need to handle tuple arguments
    393                                 assert( arg->has_result() );
    394                                 Type * result = arg->get_result();
    395                                 if ( skipCopyConstruct( result ) ) continue; // skip certain non-copyable types
    396                                 // type may involve type variables, so apply type substitution to get temporary variable's actual type
    397                                 result = result->clone();
    398                                 impCpCtorExpr->get_env()->apply( result );
    399                                 ObjectDecl * tmp = new ObjectDecl( tempNamer.newName(), DeclarationNode::NoStorageClass, LinkageSpec::C, 0, result, 0 );
    400                                 tmp->get_type()->set_isConst( false );
    401 
    402                                 // create and resolve copy constructor
    403                                 CP_CTOR_PRINT( std::cerr << "makeCtorDtor for an argument" << std::endl; )
    404                                 ApplicationExpr * cpCtor = makeCtorDtor( "?{}", tmp, arg );
    405 
    406                                 // if the chosen constructor is intrinsic, the copy is unnecessary, so
    407                                 // don't create the temporary and don't call the copy constructor
    408                                 VariableExpr * function = dynamic_cast< VariableExpr * >( cpCtor->get_function() );
    409                                 assert( function );
    410                                 if ( function->get_var()->get_linkage() != LinkageSpec::Intrinsic ) {
    411                                         // replace argument to function call with temporary
    412                                         arg = new CommaExpr( cpCtor, new VariableExpr( tmp ) );
    413                                         impCpCtorExpr->get_tempDecls().push_back( tmp );
    414                                         impCpCtorExpr->get_dtors().push_front( makeCtorDtor( "^?{}", tmp ) );
    415                                 } // if
     444                                copyConstructArg( arg, impCpCtorExpr );
    416445                        } // for
    417446
     
    425454                        Type * result = appExpr->get_result();
    426455                        if ( ! result->isVoid() ) {
    427                                 // need to flatten result type and construct each
     456                                static UniqueName retNamer("_tmp_cp_ret");
    428457                                result = result->clone();
    429458                                impCpCtorExpr->get_env()->apply( result );
     
    432461                                impCpCtorExpr->get_returnDecls().push_back( ret );
    433462                                CP_CTOR_PRINT( std::cerr << "makeCtorDtor for a return" << std::endl; )
    434                                 impCpCtorExpr->get_dtors().push_front( makeCtorDtor( "^?{}", ret ) );
     463                                destructRet( new VariableExpr( ret ) , impCpCtorExpr );
    435464                        } // for
    436465                        CP_CTOR_PRINT( std::cerr << "after Resolving: " << impCpCtorExpr << std::endl; )
Note: See TracChangeset for help on using the changeset viewer.