Changeset 0a22cda for src


Ignore:
Timestamp:
Oct 19, 2017, 11:13:11 AM (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:
6137fbb
Parents:
eaa2edaa
git-author:
Rob Schluntz <rschlunt@…> (10/11/17 15:57:07)
git-committer:
Rob Schluntz <rschlunt@…> (10/19/17 11:13:11)
Message:

Remove unnecessary resolver-generated initialization casts

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/ResolvExpr/Resolver.cc

    reaa2edaa r0a22cda  
    113113                        env.makeSubstitution( *expr->get_env() );
    114114                }
     115
     116                void removeExtraneousCast( Expression *& expr, const SymTab::Indexer & indexer ) {
     117                        if ( CastExpr * castExpr = dynamic_cast< CastExpr * >( expr ) ) {
     118                                if ( ResolvExpr::typesCompatible( castExpr->arg->result, castExpr->result, indexer ) ) {
     119                                        // cast is to the same type as its argument, so it's unnecessary -- remove it
     120                                        expr = castExpr->arg;
     121                                        castExpr->arg = nullptr;
     122                                        std::swap( expr->env, castExpr->env );
     123                                        delete castExpr;
     124                                }
     125                        }
     126                }
    115127        } // namespace
    116128
     
    151163                untyped = new CastExpr( untyped, type );
    152164                findSingleExpression( untyped, indexer );
    153                 if ( CastExpr * castExpr = dynamic_cast< CastExpr * >( untyped ) ) {
    154                         if ( ResolvExpr::typesCompatible( castExpr->arg->result, castExpr->result, indexer ) ) {
    155                                 // cast is to the same type as its argument, so it's unnecessary -- remove it
    156                                 untyped = castExpr->arg;
    157                                 castExpr->arg = nullptr;
    158                                 delete castExpr;
    159                         }
    160                 }
     165                removeExtraneousCast( untyped, indexer );
    161166        }
    162167
     
    577582                visit_children = false;
    578583                // resolve initialization using the possibilities as determined by the currentObject cursor
    579                 Expression * newExpr = new UntypedInitExpr( singleInit->get_value(), currentObject.getOptions() );
     584                Expression * newExpr = new UntypedInitExpr( singleInit->value, currentObject.getOptions() );
    580585                findSingleExpression( newExpr, indexer );
    581586                InitExpr * initExpr = strict_dynamic_cast< InitExpr * >( newExpr );
     
    592597                // get the actual object's type (may not exactly match what comes back from the resolver due to conversions)
    593598                Type * initContext = currentObject.getCurrentType();
     599
     600                removeExtraneousCast( newExpr, indexer );
    594601
    595602                // check if actual object's type is char[]
     
    599606                                if ( PointerType * pt = dynamic_cast< PointerType *>( newExpr->get_result() ) ) {
    600607                                        if ( isCharType( pt->get_base() ) ) {
    601                                                 // strip cast if we're initializing a char[] with a char *, e.g.  char x[] = "hello";
    602                                                 CastExpr *ce = strict_dynamic_cast< CastExpr * >( newExpr );
    603                                                 newExpr = ce->get_arg();
    604                                                 ce->set_arg( nullptr );
    605                                                 delete ce;
     608                                                if ( CastExpr *ce = dynamic_cast< CastExpr * >( newExpr ) ) {
     609                                                        // strip cast if we're initializing a char[] with a char *, e.g.  char x[] = "hello";
     610                                                        newExpr = ce->get_arg();
     611                                                        ce->set_arg( nullptr );
     612                                                        std::swap( ce->env, newExpr->env );
     613                                                        delete ce;
     614                                                }
    606615                                        }
    607616                                }
     
    610619
    611620                // set initializer expr to resolved express
    612                 singleInit->set_value( newExpr );
     621                singleInit->value = newExpr;
    613622
    614623                // move cursor to next object in preparation for next initializer
Note: See TracChangeset for help on using the changeset viewer.