Changeset 5ccc733 for src/ResolvExpr


Ignore:
Timestamp:
Jun 29, 2024, 5:02:06 AM (3 weeks ago)
Author:
JiadaL <j82liang@…>
Branches:
master
Children:
4117761
Parents:
7552fde
Message:

Fix the bug that C style enum cannot to use as an lvalue

Location:
src/ResolvExpr
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • src/ResolvExpr/CastCost.cpp

    r7552fde r5ccc733  
    5454                        cost = conversionCost( enumInst, dst, srcIsLvalue, symtab, env );
    5555
    56                         if (Cost::unsafe < cost) {
     56                        if ( !enumInst->base->isCfa ) {
    5757                                static ast::ptr<ast::BasicType> integer = { new ast::BasicType( ast::BasicKind::SignedInt ) };
    5858                                Cost intCost = costCalc( integer, dst, srcIsLvalue, symtab, env );
    5959                                cost = intCost < cost? intCost: cost;
    60                         }
    61                         if ( enumInst->base->isTyped && enumInst->base->base ) {
     60                        } else if ( enumInst->base->isTyped() ) {
    6261                                auto baseConversionCost =
    6362                                        castCost( enumInst->base->base, dst, srcIsLvalue, symtab, env );
     
    7473                                cost = conversionCost( basicType, dst, srcIsLvalue, symtab, env );
    7574                                if ( Cost::unsafe < cost ) {
    76                                         if (auto enumInst = dynamic_cast<const ast::EnumInstType *>(dst)) {
    77                                                 // Always explict cast only for typed enum
    78                                                 if (enumInst->base->isTyped) cost = Cost::unsafe;
     75                                        if ( dynamic_cast<const ast::EnumInstType *>(dst)) {
     76                                                cost = Cost::unsafe;
    7977                                        }
    8078                                }
     
    8583                        cost = conversionCost( zero, dst, srcIsLvalue, symtab, env );
    8684                        if ( Cost::unsafe < cost ) {
    87                                 if (auto enumInst = dynamic_cast<const ast::EnumInstType *>(dst)) {
    88                                         if (enumInst->base->isTyped) cost = Cost::unsafe;
     85                                if ( dynamic_cast<const ast::EnumInstType *>(dst)) {
     86                                        cost = Cost::unsafe;
    8987                                }
    9088                        }
     
    9492                        cost = conversionCost( one, dst, srcIsLvalue, symtab, env );
    9593                        if ( Cost::unsafe < cost ) {
    96                                 if (auto enumInst = dynamic_cast<const ast::EnumInstType *>(dst)) {
    97                                         if (enumInst->base->isTyped) cost = Cost::unsafe;
     94                                if ( dynamic_cast<const ast::EnumInstType *>(dst)) {
     95                                        cost = Cost::unsafe;
    9896                                }
    9997                        }
  • src/ResolvExpr/CommonType.cpp

    r7552fde r5ccc733  
    386386                } else if ( const ast::EnumInstType * enumInst = dynamic_cast< const ast::EnumInstType * >( type2 ) ) {
    387387                        const ast::EnumDecl* enumDecl = enumInst->base;
    388                         if ( !enumDecl->base ) {
     388                        if ( !enumDecl->isCfa ) {
    389389                                ast::BasicKind kind = commonTypes[ basic->kind ][ ast::BasicKind::SignedInt ];
    390390                                if (
     
    642642                        const ast::EnumDecl* argDecl = argAsEnumInst->base;
    643643                        if (argDecl->isSubTypeOf(paramDecl)) result = param;
    644                 } else if ( param->base && !param->base->isTyped ) {
     644                } else if ( param->base && !param->base->isCfa ) {
    645645                        auto basicType = new ast::BasicType( ast::BasicKind::UnsignedInt );
    646646                        result = commonType( basicType, type2, tenv, need, have, open, widen);
  • src/ResolvExpr/ConversionCost.cpp

    r7552fde r5ccc733  
    235235                        return ast::Pass<ConversionCost>::read( src, dst, srcIsLvalue, symtab, env, conversionCost );
    236236                }
     237                if (const ast::EnumInstType * srcAsInst = dynamic_cast< const ast::EnumInstType * >( src )) {
     238                        if (srcAsInst->base && !srcAsInst->base->isCfa) {
     239                                static const ast::BasicType* integer = new ast::BasicType( ast::BasicKind::UnsignedInt );
     240                                return ast::Pass<ConversionCost>::read( integer, dst, srcIsLvalue, symtab, env, conversionCost );
     241                        }
     242                }
    237243        } else {
    238244                assert( -1 == diff );
    239245                const ast::ReferenceType * dstAsRef = dynamic_cast< const ast::ReferenceType * >( dst );
    240246                assert( dstAsRef );
    241                 if ( typesCompatibleIgnoreQualifiers( src, dstAsRef->base, env ) ) {
     247                auto dstBaseType = dstAsRef->base;
     248                const ast::Type * newSrc = src;
     249                if ( dynamic_cast< const ast::EnumInstType * >( src ) && dstBaseType.as<ast::BasicType>() ) {
     250                        newSrc = new ast::BasicType( ast::BasicKind::UnsignedInt );
     251                }
     252                if ( typesCompatibleIgnoreQualifiers( newSrc, dstAsRef->base, env ) ) {
    242253                        if ( srcIsLvalue ) {
    243254                                if ( src->qualifiers == dstAsRef->base->qualifiers ) {
     
    285296                conversionCostFromBasicToBasic( basicType, dstAsBasic );
    286297        } else if ( auto dstAsEnumInst = dynamic_cast< const ast::EnumInstType * >( dst ) ) {
    287                 if ( dstAsEnumInst->base && !dstAsEnumInst->base->isTyped ) {
    288                         cost = Cost::unsafe;
     298                if ( dstAsEnumInst->base && !dstAsEnumInst->base->isCfa ) {
     299                        cost = Cost::safe;
    289300                }
    290301        }
     
    366377        if ( auto dstInst = dynamic_cast<const ast::EnumInstType *>( dst ) ) {
    367378                cost = enumCastCost(inst, dstInst, symtab, env);
    368                 return;
    369         }
    370 
    371         if ( !inst->base->isTyped ) {
     379        } else if ( !inst->base->isCfa ) {
    372380                static ast::ptr<ast::BasicType> integer = { new ast::BasicType( ast::BasicKind::SignedInt ) };
    373381                cost = costCalc( integer, dst, srcIsLvalue, symtab, env );
    374                 if ( cost < Cost::unsafe ) {
    375                         cost.incSafe();
    376                 }
    377                 return;
    378382        }
    379383        // cost.incUnsafe();
     
    455459                // assuming 0p is supposed to be used for pointers?
    456460        } else if ( auto dstAsEnumInst = dynamic_cast< const ast::EnumInstType * >( dst ) ) {
    457                 if ( dstAsEnumInst->base && !dstAsEnumInst->base->isTyped ) {
    458                         cost = Cost::unsafe;
     461                if ( dstAsEnumInst->base && !dstAsEnumInst->base->isCfa ) {
     462                        cost = Cost::safe;
    459463                }
    460464        }
     
    476480                }
    477481        } else if ( auto dstAsEnumInst = dynamic_cast< const ast::EnumInstType * >( dst ) ) {
    478                 if ( dstAsEnumInst->base && !dstAsEnumInst->base->isTyped ) {
    479                         cost = Cost::unsafe;
     482                if ( dstAsEnumInst->base && !dstAsEnumInst->base->isCfa ) {
     483                        cost = Cost::safe;
    480484                }
    481485        }
Note: See TracChangeset for help on using the changeset viewer.