Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/ResolvExpr/ConversionCost.cc

    r76fe046 r748c751  
    278278        if ( const ast::BasicType * dstAsBasic = dynamic_cast< const ast::BasicType * >( dst ) ) {
    279279                conversionCostFromBasicToBasic( basicType, dstAsBasic );
    280         }
    281         else if ( const ast::EnumInstType * enumInst = dynamic_cast< const ast::EnumInstType * >( dst ) ) {
    282                 auto enumDecl = enumInst->base;
    283                 if ( enumDecl->base.get() ) {
    284                         // cost = costCalc( basicType, baseType, srcIsLvalue, symtab, env );
    285                         // cost.incUnsafe();
    286                         cost = Cost::infinity;
    287                 } else {
    288             cost = Cost::unsafe;
    289                 }
    290         } else if ( dynamic_cast< const ast::EnumPosType *>(dst) ) {
     280        } else if ( dynamic_cast< const ast::EnumAttrType *>(dst) ) {
    291281                static ast::ptr<ast::BasicType> integer = { new ast::BasicType( ast::BasicType::SignedInt ) };
    292282                cost = costCalc( basicType, integer, srcIsLvalue, symtab, env );
     283        } else if ( auto dstAsEnumInst = dynamic_cast< const ast::EnumInstType * >( dst ) ) {
     284                if ( dstAsEnumInst->base && !dstAsEnumInst->base->base ) {
     285                        cost = Cost::zero;
     286                        cost.incUnsafe();
     287                }
    293288        }
    294289}
     
    366361}
    367362
    368 void ConversionCost::postvisit( const ast::EnumInstType * ) {
     363void ConversionCost::postvisit( const ast::EnumInstType * inst ) {
     364        if ( auto dstAsInst = dynamic_cast<const ast::EnumInstType *>( dst ) ) {
     365                if (inst->base && dstAsInst->base) {
     366                        if (inst->base->name == dstAsInst->base->name) {
     367                                cost = Cost::zero;
     368                                return;
     369                        }
     370                }
     371        }
    369372        static ast::ptr<ast::BasicType> integer = { new ast::BasicType( ast::BasicType::SignedInt ) };
    370373        cost = costCalc( integer, dst, srcIsLvalue, symtab, env );
     
    374377}
    375378
    376 void ConversionCost::postvisit( const ast::EnumPosType * src ) {
    377         if ( dynamic_cast<const ast::EnumPosType *>( dst ) ) {
    378                 // cost = costCalc( src->instance, dstBase->instance, srcIsLvalue, symtab, env );
    379                 // if ( cost < Cost::unsafe ) cost.incSafe();
    380                 cost = Cost::zero;
    381         } else if ( auto dstBase = dynamic_cast<const ast::EnumInstType *>( dst ) ) {
    382                 cost = costCalc( src->instance, dstBase, srcIsLvalue, symtab, env );
    383                 if ( cost < Cost::unsafe ) cost.incSafe();
    384         } else {
    385                 static ast::ptr<ast::BasicType> integer = { new ast::BasicType( ast::BasicType::SignedInt ) };
    386                 cost = costCalc( integer, dst, srcIsLvalue, symtab, env );
    387                 if ( cost < Cost::unsafe ) {
    388                         cost.incSafe();
    389                 }
    390         }
    391 
     379void ConversionCost::postvisit( const ast::EnumAttrType * src ) {
     380    auto dstAsEnumAttrType = dynamic_cast<const ast::EnumAttrType *>(dst);
     381        assert( src->attr != ast::EnumAttribute::Label );
     382    if ( src->attr == ast::EnumAttribute::Value ) {
     383        if ( dstAsEnumAttrType && dstAsEnumAttrType->attr == ast::EnumAttribute::Value) {
     384            cost = costCalc( src->instance, dstAsEnumAttrType->instance, srcIsLvalue, symtab, env );
     385        } else {
     386            auto baseType = src->instance->base->base;
     387            cost = costCalc( baseType, dst, srcIsLvalue, symtab, env );
     388                        if ( cost < Cost::infinity ) {
     389                                cost.incUnsafe();
     390                        }
     391        }
     392    } else { // ast::EnumAttribute::Posn
     393        if ( auto dstBase = dynamic_cast<const ast::EnumInstType *>( dst ) ) {
     394                    cost = costCalc( src->instance, dstBase, srcIsLvalue, symtab, env );
     395                    if ( cost < Cost::unsafe ) cost.incSafe();
     396            } else {
     397                    static ast::ptr<ast::BasicType> integer = { new ast::BasicType( ast::BasicType::SignedInt ) };
     398                    cost = costCalc( integer, dst, srcIsLvalue, symtab, env );
     399                    if ( cost < Cost::unsafe ) {
     400                            cost.incSafe();
     401                    }
     402            }
     403    }
    392404}
    393405
     
    466478                cost.incSafe( maxIntCost + 2 );
    467479                // assuming 0p is supposed to be used for pointers?
     480        } else if ( auto dstAsEnumInst = dynamic_cast< const ast::EnumInstType * >( dst ) ) {
     481                if ( dstAsEnumInst->base && !dstAsEnumInst->base->base ) {
     482                        cost = Cost::zero;
     483                        cost.incUnsafe();
     484                }
    468485        }
    469486}
     
    483500                        cost.incSign( signMatrix[ ast::BasicType::SignedInt ][ dstAsBasic->kind ] );
    484501                }
     502        } else if ( auto dstAsEnumInst = dynamic_cast< const ast::EnumInstType * >( dst ) ) {
     503                if ( dstAsEnumInst->base && !dstAsEnumInst->base->base ) {
     504                        cost = Cost::zero;
     505                        cost.incUnsafe();
     506                }
    485507        }
    486508}
Note: See TracChangeset for help on using the changeset viewer.