Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/ResolvExpr/ConversionCost.cc

    rfc134a48 rf238fcc2  
    321321        }
    322322
    323         // refactor for code resue
    324         void ConversionCost::conversionCostFromBasicToBasic(const BasicType * src, const BasicType * dest) {
    325                 int tableResult = costMatrix[ src->kind ][ dest->kind ];
    326                 if ( tableResult == -1 ) {
    327                         cost = Cost::unsafe;
    328                 } else {
    329                         cost = Cost::zero;
    330                         cost.incSafe( tableResult );
    331                         cost.incSign( signMatrix[ src->kind ][ dest->kind ] );
    332                 } // if
    333         } // ConversionCost::conversionCostFromBasicToBasic
    334 
    335323        void ConversionCost::postvisit(const BasicType * basicType) {
    336324                if ( const BasicType * destAsBasic = dynamic_cast< const BasicType * >( dest ) ) {
    337                         conversionCostFromBasicToBasic(basicType, destAsBasic);
    338                 } else if ( const EnumInstType * enumInst = dynamic_cast< const EnumInstType * >( dest ) ) {
    339                         const EnumDecl * base_enum = enumInst->baseEnum;
    340                         if ( const Type * base = base_enum->base ) { // if the base enum has a base (if it is typed)
    341                                 if ( const BasicType * enumBaseAstBasic = dynamic_cast< const BasicType *> (base) ) {
    342                                         conversionCostFromBasicToBasic(basicType, enumBaseAstBasic);
    343                                 } else {
    344                                         cost = Cost::infinity;
    345                                 } // if
     325                        int tableResult = costMatrix[ basicType->kind ][ destAsBasic->kind ];
     326                        if ( tableResult == -1 ) {
     327                                cost = Cost::unsafe;
     328                        } else {
     329                                cost = Cost::zero;
     330                                cost.incSafe( tableResult );
     331                                cost.incSign( signMatrix[ basicType->kind ][ destAsBasic->kind ] );
     332                        } // if
     333                } else if ( dynamic_cast< const EnumInstType * >( dest ) ) {
     334                        // xxx - not positive this is correct, but appears to allow casting int => enum
     335                        // TODO
     336                        EnumDecl * decl = dynamic_cast< const EnumInstType * >( dest )->baseEnum;
     337                        if ( decl->base ) {
     338                                cost = Cost::infinity;
    346339                        } else {
    347340                                cost = Cost::unsafe;
     
    405398        void ConversionCost::postvisit( const FunctionType * ) {}
    406399
    407         void ConversionCost::postvisit( const EnumInstType * enumInst) {
    408                 const EnumDecl * enumDecl = enumInst -> baseEnum;
    409                 if ( const Type * enumType = enumDecl -> base ) { // if it is a typed enum
    410                         cost = costFunc( enumType, dest, srcIsLvalue, indexer, env );
    411                 } else {
    412                         static Type::Qualifiers q;
    413                         static BasicType integer( q, BasicType::SignedInt );
    414                         cost = costFunc( &integer, dest, srcIsLvalue, indexer, env );  // safe if dest >= int
    415                 } // if
     400        void ConversionCost::postvisit( const EnumInstType * ) {
     401                static Type::Qualifiers q;
     402                static BasicType integer( q, BasicType::SignedInt );
     403                cost = costFunc( &integer, dest, srcIsLvalue, indexer, env );  // safe if dest >= int
    416404                if ( cost < Cost::unsafe ) {
    417                                 cost.incSafe();
     405                        cost.incSafe();
    418406                } // if
    419407        }
     
    616604}
    617605
    618 void ConversionCost_new::conversionCostFromBasicToBasic( const ast::BasicType * src, const ast::BasicType* dest ) {
    619         int tableResult = costMatrix[ src->kind ][ dest->kind ];
    620         if ( tableResult == -1 ) {
    621                 cost = Cost::unsafe;
    622         } else {
    623                 cost = Cost::zero;
    624                 cost.incSafe( tableResult );
    625                 cost.incSign( signMatrix[ src->kind ][ dest->kind ] );
    626         }
    627 }
    628 
    629606void ConversionCost_new::postvisit( const ast::BasicType * basicType ) {
    630607        if ( const ast::BasicType * dstAsBasic = dynamic_cast< const ast::BasicType * >( dst ) ) {
    631                 conversionCostFromBasicToBasic( basicType, dstAsBasic );
    632         } else if ( const ast::EnumInstType * enumInst = dynamic_cast< const ast::EnumInstType * >( dst ) ) {
    633                 const ast::EnumDecl * enumDecl = enumInst->base.get();
    634                 if ( const ast::Type * enumType = enumDecl->base.get() ) {
    635                         if ( const ast::BasicType * enumTypeAsBasic = dynamic_cast<const ast::BasicType *>(enumType) ) {
    636                                 conversionCostFromBasicToBasic( basicType, enumTypeAsBasic );
    637                         } else {
    638                                 cost = Cost::infinity;
    639                         }
    640                 } else {
    641             cost = Cost::unsafe;
    642                 }
     608                int tableResult = costMatrix[ basicType->kind ][ dstAsBasic->kind ];
     609                if ( tableResult == -1 ) {
     610                        cost = Cost::unsafe;
     611                } else {
     612                        cost = Cost::zero;
     613                        cost.incSafe( tableResult );
     614                        cost.incSign( signMatrix[ basicType->kind ][ dstAsBasic->kind ] );
     615                }
     616        } else if ( dynamic_cast< const ast::EnumInstType * >( dst ) ) {
     617                // xxx - not positive this is correct, but appears to allow casting int => enum
     618                const ast::EnumDecl * decl = (dynamic_cast< const ast::EnumInstType * >( dst ))->base.get();
     619                if ( decl->base ) {
     620                        cost = Cost::infinity;
     621                } else {
     622                        cost = Cost::unsafe;
     623                } // if
    643624        }
    644625}
     
    692673
    693674void ConversionCost_new::postvisit( const ast::EnumInstType * enumInstType ) {
    694         const ast::EnumDecl * baseEnum = enumInstType->base;
    695         if ( const ast::Type * baseType = baseEnum->base ) {
    696                 cost = costCalc( baseType, dst, srcIsLvalue, symtab, env );
    697         } else {
    698                 (void)enumInstType;
    699                 static ast::ptr<ast::BasicType> integer = { new ast::BasicType( ast::BasicType::SignedInt ) };
    700                 cost = costCalc( integer, dst, srcIsLvalue, symtab, env );
    701         }
     675        (void)enumInstType;
     676        static ast::ptr<ast::BasicType> integer = { new ast::BasicType( ast::BasicType::SignedInt ) };
     677        cost = costCalc( integer, dst, srcIsLvalue, symtab, env );
    702678        if ( cost < Cost::unsafe ) {
    703679                cost.incSafe();
Note: See TracChangeset for help on using the changeset viewer.