Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/ResolvExpr/Resolver.cc

    r08da53d r3c398b6  
    109109
    110110        namespace {
    111                 void finishExpr( Expression *expr, const TypeEnvironment &env, TypeSubstitution * oldenv = nullptr ) {
    112                         expr->env = oldenv ? oldenv->clone() : new TypeSubstitution;
     111                void finishExpr( Expression *expr, const TypeEnvironment &env ) {
     112                        expr->set_env( new TypeSubstitution );
    113113                        env.makeSubstitution( *expr->get_env() );
    114114                }
    115115        } // namespace
    116116
    117         void findVoidExpression( Expression *& untyped, const SymTab::Indexer &indexer ) {
     117        Expression *findVoidExpression( Expression *untyped, const SymTab::Indexer &indexer ) {
    118118                global_renamer.reset();
    119119                TypeEnvironment env;
    120120                Expression *newExpr = resolveInVoidContext( untyped, indexer, env );
    121                 finishExpr( newExpr, env, untyped->env );
    122                 delete untyped;
    123                 untyped = newExpr;
    124         }
    125 
    126         void findSingleExpression( Expression *&untyped, const SymTab::Indexer &indexer ) {
    127                 if ( ! untyped ) return;
     121                finishExpr( newExpr, env );
     122                return newExpr;
     123        }
     124
     125        Expression * findSingleExpression( Expression *untyped, const SymTab::Indexer &indexer ) {
    128126                TypeEnvironment env;
    129127                AlternativeFinder finder( indexer, env );
     
    131129                #if 0
    132130                if ( finder.get_alternatives().size() != 1 ) {
    133                         std::cerr << "untyped expr is ";
    134                         untyped->print( std::cerr );
    135                         std::cerr << std::endl << "alternatives are:";
    136                         for ( const Alternative & alt : finder.get_alternatives() ) {
    137                                 alt.print( std::cerr );
     131                        std::cout << "untyped expr is ";
     132                        untyped->print( std::cout );
     133                        std::cout << std::endl << "alternatives are:";
     134                        for ( std::list< Alternative >::const_iterator i = finder.get_alternatives().begin(); i != finder.get_alternatives().end(); ++i ) {
     135                                i->print( std::cout );
    138136                        } // for
    139137                } // if
     
    142140                Alternative &choice = finder.get_alternatives().front();
    143141                Expression *newExpr = choice.expr->clone();
    144                 finishExpr( newExpr, choice.env, untyped->env );
    145                 delete untyped;
    146                 untyped = newExpr;
    147         }
    148 
    149         void findSingleExpression( Expression *& untyped, Type * type, const SymTab::Indexer & indexer ) {
    150                 assert( untyped && type );
    151                 untyped = new CastExpr( untyped, type );
    152                 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                 }
     142                finishExpr( newExpr, choice.env );
     143                return newExpr;
    161144        }
    162145
     
    174157                }
    175158
    176                 void findIntegralExpression( Expression *& untyped, const SymTab::Indexer &indexer ) {
     159                Expression *findIntegralExpression( Expression *untyped, const SymTab::Indexer &indexer ) {
    177160                        TypeEnvironment env;
    178161                        AlternativeFinder finder( indexer, env );
     
    203186                                throw SemanticError( "No interpretations for case control expression", untyped );
    204187                        } // if
    205                         finishExpr( newExpr, *newEnv, untyped->env );
    206                         delete untyped;
    207                         untyped = newExpr;
     188                        finishExpr( newExpr, *newEnv );
     189                        return newExpr;
    208190                }
    209191
     
    230212        void Resolver::handlePtrType( PtrType * type ) {
    231213                if ( type->get_dimension() ) {
    232                         findSingleExpression( type->dimension, SymTab::SizeType->clone(), indexer );
     214                        CastExpr *castExpr = new CastExpr( type->get_dimension(), SymTab::SizeType->clone() );
     215                        Expression *newExpr = findSingleExpression( castExpr, indexer );
     216                        delete type->get_dimension();
     217                        type->set_dimension( newExpr );
    233218                }
    234219        }
     
    283268        void Resolver::previsit( ExprStmt *exprStmt ) {
    284269                visit_children = false;
    285                 assertf( exprStmt->expr, "ExprStmt has null Expression in resolver" );
    286                 findVoidExpression( exprStmt->expr, indexer );
     270                assertf( exprStmt->get_expr(), "ExprStmt has null Expression in resolver" );
     271                Expression *newExpr = findVoidExpression( exprStmt->get_expr(), indexer );
     272                delete exprStmt->get_expr();
     273                exprStmt->set_expr( newExpr );
    287274        }
    288275
    289276        void Resolver::previsit( AsmExpr *asmExpr ) {
    290277                visit_children = false;
    291                 findVoidExpression( asmExpr->operand, indexer );
     278                Expression *newExpr = findVoidExpression( asmExpr->get_operand(), indexer );
     279                delete asmExpr->get_operand();
     280                asmExpr->set_operand( newExpr );
    292281                if ( asmExpr->get_inout() ) {
    293                         findVoidExpression( asmExpr->inout, indexer );
     282                        newExpr = findVoidExpression( asmExpr->get_inout(), indexer );
     283                        delete asmExpr->get_inout();
     284                        asmExpr->set_inout( newExpr );
    294285                } // if
    295286        }
     
    302293
    303294        void Resolver::previsit( IfStmt *ifStmt ) {
    304                 findSingleExpression( ifStmt->condition, indexer );
     295                Expression *newExpr = findSingleExpression( ifStmt->get_condition(), indexer );
     296                delete ifStmt->get_condition();
     297                ifStmt->set_condition( newExpr );
    305298        }
    306299
    307300        void Resolver::previsit( WhileStmt *whileStmt ) {
    308                 findSingleExpression( whileStmt->condition, indexer );
     301                Expression *newExpr = findSingleExpression( whileStmt->get_condition(), indexer );
     302                delete whileStmt->get_condition();
     303                whileStmt->set_condition( newExpr );
    309304        }
    310305
    311306        void Resolver::previsit( ForStmt *forStmt ) {
    312                 if ( forStmt->condition ) {
    313                         findSingleExpression( forStmt->condition, indexer );
     307                if ( forStmt->get_condition() ) {
     308                        Expression * newExpr = findSingleExpression( forStmt->get_condition(), indexer );
     309                        delete forStmt->get_condition();
     310                        forStmt->set_condition( newExpr );
    314311                } // if
    315312
    316                 if ( forStmt->increment ) {
    317                         findVoidExpression( forStmt->increment, indexer );
     313                if ( forStmt->get_increment() ) {
     314                        Expression * newExpr = findVoidExpression( forStmt->get_increment(), indexer );
     315                        delete forStmt->get_increment();
     316                        forStmt->set_increment( newExpr );
    318317                } // if
    319318        }
     
    321320        void Resolver::previsit( SwitchStmt *switchStmt ) {
    322321                GuardValue( currentObject );
    323                 findIntegralExpression( switchStmt->condition, indexer );
    324 
    325                 currentObject = CurrentObject( switchStmt->condition->result );
     322                Expression *newExpr;
     323                newExpr = findIntegralExpression( switchStmt->get_condition(), indexer );
     324                delete switchStmt->get_condition();
     325                switchStmt->set_condition( newExpr );
     326
     327                currentObject = CurrentObject( newExpr->get_result() );
    326328        }
    327329
     
    330332                        std::list< InitAlternative > initAlts = currentObject.getOptions();
    331333                        assertf( initAlts.size() == 1, "SwitchStmt did not correctly resolve an integral expression." );
    332                         // must remove cast from case statement because RangeExpr cannot be cast.
    333                         Expression * newExpr = new CastExpr( caseStmt->condition, initAlts.front().type->clone() );
    334                         findSingleExpression( newExpr, indexer );
    335                         CastExpr * castExpr = strict_dynamic_cast< CastExpr * >( newExpr );
    336                         caseStmt->condition = castExpr->arg;
    337                         castExpr->arg = nullptr;
     334                        CastExpr * castExpr = new CastExpr( caseStmt->get_condition(), initAlts.front().type->clone() );
     335                        Expression * newExpr = findSingleExpression( castExpr, indexer );
     336                        castExpr = strict_dynamic_cast< CastExpr * >( newExpr );
     337                        caseStmt->set_condition( castExpr->get_arg() );
     338                        castExpr->set_arg( nullptr );
    338339                        delete castExpr;
    339340                }
     
    344345                // must resolve the argument for a computed goto
    345346                if ( branchStmt->get_type() == BranchStmt::Goto ) { // check for computed goto statement
    346                         if ( branchStmt->computedTarget ) {
    347                                 // computed goto argument is void *
    348                                 findSingleExpression( branchStmt->computedTarget, new PointerType( Type::Qualifiers(), new VoidType( Type::Qualifiers() ) ), indexer );
     347                        if ( Expression * arg = branchStmt->get_computedTarget() ) {
     348                                VoidType v = Type::Qualifiers();                // cast to void * for the alternative finder
     349                                PointerType pt( Type::Qualifiers(), v.clone() );
     350                                CastExpr * castExpr = new CastExpr( arg, pt.clone() );
     351                                Expression * newExpr = findSingleExpression( castExpr, indexer ); // find best expression
     352                                branchStmt->set_target( newExpr );
    349353                        } // if
    350354                } // if
     
    353357        void Resolver::previsit( ReturnStmt *returnStmt ) {
    354358                visit_children = false;
    355                 if ( returnStmt->expr ) {
    356                         findSingleExpression( returnStmt->expr, functionReturn->clone(), indexer );
     359                if ( returnStmt->get_expr() ) {
     360                        CastExpr *castExpr = new CastExpr( returnStmt->get_expr(), functionReturn->clone() );
     361                        Expression *newExpr = findSingleExpression( castExpr, indexer );
     362                        delete castExpr;
     363                        returnStmt->set_expr( newExpr );
    357364                } // if
    358365        }
     
    365372                                indexer.lookupStruct( "__cfaehm__base_exception_t" );
    366373                        assert( exception_decl );
    367                         Type * exceptType = new PointerType( noQualifiers, new StructInstType( noQualifiers, exception_decl ) );
    368                         findSingleExpression( throwStmt->expr, exceptType, indexer );
     374                        Expression * wrapped = new CastExpr(
     375                                throwStmt->get_expr(),
     376                                new PointerType(
     377                                        noQualifiers,
     378                                        new StructInstType(
     379                                                noQualifiers,
     380                                                exception_decl
     381                                                )
     382                                        )
     383                                );
     384                        Expression * newExpr = findSingleExpression( wrapped, indexer );
     385                        throwStmt->set_expr( newExpr );
    369386                }
    370387        }
    371388
    372389        void Resolver::previsit( CatchStmt *catchStmt ) {
    373                 if ( catchStmt->cond ) {
    374                         findSingleExpression( catchStmt->cond, new BasicType( noQualifiers, BasicType::Bool ), indexer );
    375                 }
     390                if ( catchStmt->get_cond() ) {
     391                        Expression * wrapped = new CastExpr(
     392                                catchStmt->get_cond(),
     393                                new BasicType( noQualifiers, BasicType::Bool )
     394                                );
     395                        catchStmt->set_cond( findSingleExpression( wrapped, indexer ) );
     396                }
     397        }
     398
     399        inline void resolveAsIf( Expression *& expr, SymTab::Indexer & indexer ) {
     400                if( !expr ) return;
     401                Expression * newExpr = findSingleExpression( expr, indexer );
     402                delete expr;
     403                expr = newExpr;
     404        }
     405
     406        inline void resolveAsType( Expression *& expr, Type * type, SymTab::Indexer & indexer ) {
     407                if( !expr ) return;
     408                Expression * newExpr = findSingleExpression( new CastExpr( expr, type ), indexer );
     409                delete expr;
     410                expr = newExpr;
    376411        }
    377412
     
    543578                        // Resolve the conditions as if it were an IfStmt
    544579                        // Resolve the statments normally
    545                         findSingleExpression( clause.condition, this->indexer );
     580                        resolveAsIf( clause.condition, this->indexer );
    546581                        clause.statement->accept( *visitor );
    547582                }
     
    552587                        // Resolve the conditions as if it were an IfStmt
    553588                        // Resolve the statments normally
    554                         findSingleExpression( stmt->timeout.time, new BasicType( noQualifiers, BasicType::LongLongUnsignedInt ), this->indexer );
    555                         findSingleExpression( stmt->timeout.condition, this->indexer );
     589                        resolveAsType( stmt->timeout.time, new BasicType( noQualifiers, BasicType::LongLongUnsignedInt ), this->indexer );
     590                        resolveAsIf  ( stmt->timeout.condition, this->indexer );
    556591                        stmt->timeout.statement->accept( *visitor );
    557592                }
     
    560595                        // Resolve the conditions as if it were an IfStmt
    561596                        // Resolve the statments normally
    562                         findSingleExpression( stmt->orelse.condition, this->indexer );
     597                        resolveAsIf( stmt->orelse.condition, this->indexer );
    563598                        stmt->orelse.statement->accept( *visitor );
    564599                }
     
    577612                visit_children = false;
    578613                // resolve initialization using the possibilities as determined by the currentObject cursor
    579                 Expression * newExpr = new UntypedInitExpr( singleInit->get_value(), currentObject.getOptions() );
    580                 findSingleExpression( newExpr, indexer );
     614                UntypedInitExpr * untyped = new UntypedInitExpr( singleInit->get_value(), currentObject.getOptions() );
     615                Expression * newExpr = findSingleExpression( untyped, indexer );
    581616                InitExpr * initExpr = strict_dynamic_cast< InitExpr * >( newExpr );
    582617
     
    585620
    586621                // discard InitExpr wrapper and retain relevant pieces
    587                 newExpr = initExpr->expr;
    588                 initExpr->expr = nullptr;
    589                 std::swap( initExpr->env, newExpr->env );
     622                newExpr = initExpr->get_expr();
     623                newExpr->set_env( initExpr->get_env() );
     624                initExpr->set_expr( nullptr );
     625                initExpr->set_env( nullptr );
    590626                delete initExpr;
    591627
Note: See TracChangeset for help on using the changeset viewer.