Ignore:
Timestamp:
Apr 25, 2016, 2:49:55 PM (8 years ago)
Author:
Rob Schluntz <rschlunt@…>
Branches:
ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, ctor, deferred_resn, demangler, enum, forall-pointer-decay, gc_noraii, jacob/cs343-translation, jenkins-sandbox, master, memory, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
Children:
add371c
Parents:
67b1180
Message:

don't generate copy constructor calls for arguments to intrinsic functions, copy constructors, assignment operators, and destructors, fix problem with copying result type of ImplicitCopyCtorExpr? which caused expression resolution to fail

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/InitTweak/FixInit.cc

    r67b1180 r845cedc  
    1010// Created On       : Wed Jan 13 16:29:30 2016
    1111// Last Modified By : Rob Schluntz
    12 // Last Modified On : Thu Apr 14 17:29:21 2016
     12// Last Modified On : Mon Apr 25 14:44:21 2016
    1313// Update Count     : 30
    1414//
     
    1818#include "RemoveInit.h"
    1919#include "ResolvExpr/Resolver.h"
     20#include "ResolvExpr/typeops.h"
    2021#include "SynTree/Declaration.h"
    2122#include "SynTree/Type.h"
     
    2728#include "GenPoly/PolyMutator.h"
    2829
     30bool ctordtorp = false;
     31#define PRINT( text ) if ( ctordtorp ) { text }
     32
    2933namespace InitTweak {
    3034        namespace {
     
    115119
    116120        Expression * InsertImplicitCalls::mutate( ApplicationExpr * appExpr ) {
     121                appExpr = dynamic_cast< ApplicationExpr * >( Mutator::mutate( appExpr ) );
     122                assert( appExpr );
     123
    117124                if ( VariableExpr * function = dynamic_cast< VariableExpr * > ( appExpr->get_function() ) ) {
    118125                        if ( function->get_var()->get_linkage() == LinkageSpec::Intrinsic ) {
    119126                                // optimization: don't need to copy construct in order to call intrinsic functions
    120127                                return appExpr;
    121                         } else if ( FunctionDecl * func = dynamic_cast< FunctionDecl * > ( function->get_var() ) ) {
    122                                 // if (  )
    123                                 // // optimization: don't need to copy construct in order to call a copy constructor
    124                                 // return appExpr;
     128                        } else if ( FunctionDecl * funcDecl = dynamic_cast< FunctionDecl * > ( function->get_var() ) ) {
     129                                FunctionType * ftype = funcDecl->get_functionType();
     130                                if ( (funcDecl->get_name() == "?{}" || funcDecl->get_name() == "?=?") && ftype->get_parameters().size() == 2 ) {
     131                                        Type * t1 = ftype->get_parameters().front()->get_type();
     132                                        Type * t2 = ftype->get_parameters().back()->get_type();
     133                                        PointerType * ptrType = dynamic_cast< PointerType * > ( t1 );
     134                                        assert( ptrType );
     135                                        if ( ResolvExpr::typesCompatible( ptrType->get_base(), t2, SymTab::Indexer() ) ) {
     136                                                // optimization: don't need to copy construct in order to call a copy constructor or
     137                                                // assignment operator
     138                                                return appExpr;
     139                                        }
     140                                } else if ( funcDecl->get_name() == "^?{}" ) {
     141                                        // correctness: never copy construct arguments to a destructor
     142                                        return appExpr;
     143                                }
    125144                        }
    126145                }
     146                PRINT( std::cerr << "InsertImplicitCalls: adding a wrapper " << appExpr << std::endl; )
     147
    127148                // wrap each function call so that it is easy to identify nodes that have to be copy constructed
    128                 appExpr = dynamic_cast< ApplicationExpr * >( Mutator::mutate( appExpr ) );
    129                 assert( appExpr );
    130149                return new ImplicitCopyCtorExpr( appExpr );
    131150        }
     
    140159                // should only be one alternative for copy ctor and dtor expressions, since
    141160                // all arguments are fixed (VariableExpr and already resolved expression)
     161                PRINT( std::cerr << "ResolvingCtorDtor " << untyped << std::endl; )
    142162                ApplicationExpr * resolved = dynamic_cast< ApplicationExpr * >( ResolvExpr::findVoidExpression( untyped, *this ) );
    143163
     
    151171                static UniqueName retNamer("_tmp_cp_ret");
    152172
    153                 // resolve function call
    154                 ApplicationExpr * appExpr = dynamic_cast< ApplicationExpr * >( ResolvExpr::findVoidExpression( impCpCtorExpr->get_callExpr(), *this ) );
    155                 assert( appExpr );
    156                 delete impCpCtorExpr->get_callExpr();
    157                 impCpCtorExpr->set_callExpr( appExpr );
    158 
    159                 // Visitor::visit( impCpCtorExpr );
     173                PRINT( std::cerr << "ResolveCopyCtors: " << impCpCtorExpr << std::endl; )
     174                Visitor::visit( impCpCtorExpr );
     175
     176                ApplicationExpr * appExpr = impCpCtorExpr->get_callExpr();
    160177
    161178                // take each argument and attempt to copy construct it.
     
    167184
    168185                        // create and resolve copy constructor
     186                        PRINT( std::cerr << "makeCtorDtor for an argument" << std::endl; )
    169187                        ApplicationExpr * cpCtor = makeCtorDtor( "?{}", tmp, arg );
    170188
     
    186204                // later
    187205                // xxx - handle multiple return values
     206                ApplicationExpr * callExpr = impCpCtorExpr->get_callExpr();
    188207                for ( Type * result : appExpr->get_results() ) {
    189                         ObjectDecl * ret = new ObjectDecl( retNamer.newName(), DeclarationNode::NoStorageClass, LinkageSpec::C, 0, result->clone(), new SingleInit( impCpCtorExpr->get_callExpr() ) );
     208                        ObjectDecl * ret = new ObjectDecl( retNamer.newName(), DeclarationNode::NoStorageClass, LinkageSpec::C, 0, result->clone(), new SingleInit( callExpr ) );
    190209                        ret->get_type()->set_isConst( false );
    191210                        impCpCtorExpr->get_returnDecls().push_back( ret );
     211                        PRINT( std::cerr << "makeCtorDtor for a return" << std::endl; )
    192212                        impCpCtorExpr->get_dtors().push_front( makeCtorDtor( "^?{}", ret ) );
    193213                }
     214                PRINT( std::cerr << "after Resolving: " << impCpCtorExpr << std::endl; )
    194215        }
    195216
    196217
    197218        Expression * FixCopyCtors::mutate( ImplicitCopyCtorExpr * impCpCtorExpr ) {
     219                PRINT( std::cerr << "FixCopyCtors: " << impCpCtorExpr << std::endl; )
     220
     221                // assert( impCpCtorExpr->get_callExpr()->get_env() );
    198222                impCpCtorExpr = dynamic_cast< ImplicitCopyCtorExpr * >( Mutator::mutate( impCpCtorExpr ) );
    199223                assert( impCpCtorExpr );
     
    219243                // xxx - update to work with multiple return values
    220244                ObjectDecl * returnDecl = returnDecls.empty() ? NULL : returnDecls.front();
     245                Expression * callExpr = impCpCtorExpr->get_callExpr();
     246
     247                PRINT( std::cerr << "Coming out the back..." << impCpCtorExpr << std::endl; )
    221248
    222249                // xxx - some of these aren't necessary, and can be removed once this is stable
     
    225252                tempDecls.clear();
    226253                returnDecls.clear();
     254                impCpCtorExpr->set_callExpr( NULL );
     255                delete impCpCtorExpr;
    227256
    228257                if ( returnDecl ) {
     
    232261                } else {
    233262                        // add call expression - if no return values, can call directly
    234                         return impCpCtorExpr->get_callExpr();
     263                        assert( callExpr );
     264                        return callExpr;
    235265                }
    236266        }
Note: See TracChangeset for help on using the changeset viewer.