Changeset acb33f15 for src


Ignore:
Timestamp:
May 13, 2024, 10:26:52 AM (8 weeks ago)
Author:
JiadaL <j82liang@…>
Branches:
master
Children:
31f4837
Parents:
41c8312
Message:

Change enum conversion steps

Location:
src
Files:
15 edited

Legend:

Unmodified
Added
Removed
  • src/AST/Fwd.hpp

    r41c8312 racb33f15  
    133133class OneType;
    134134class GlobalScopeType;
    135 class EnumAttrType;
    136135
    137136class Designation;
  • src/AST/Pass.hpp

    r41c8312 racb33f15  
    207207        const ast::Type *             visit( const ast::UnionInstType        * ) override final;
    208208        const ast::Type *             visit( const ast::EnumInstType         * ) override final;
    209         const ast::Type *             visit( const ast::EnumAttrType         * ) override final;
    210209        const ast::Type *             visit( const ast::TraitInstType        * ) override final;
    211210        const ast::Type *             visit( const ast::TypeInstType         * ) override final;
  • src/AST/Pass.impl.hpp

    r41c8312 racb33f15  
    19401940
    19411941//--------------------------------------------------------------------------
    1942 // EnumAttrType
    1943 template< typename core_t >
    1944 const ast::Type * ast::Pass< core_t >::visit( const ast::EnumAttrType * node ) {
    1945         VISIT_START( node );
    1946         VISIT_END( Type, node );
    1947 }
    1948 
    1949 //--------------------------------------------------------------------------
    19501942// TraitInstType
    19511943template< typename core_t >
  • src/AST/Print.cpp

    r41c8312 racb33f15  
    15761576        }
    15771577
    1578         virtual const ast::Type * visit( const ast::EnumAttrType * node ) override final {
    1579                 preprint( node );
    1580                 os << "enum attr ";
    1581                 if ( node->attr == ast::EnumAttribute::Label ) {
    1582                         os << "Label ";
    1583                 } else if ( node->attr == ast::EnumAttribute::Value ) {
    1584                         os << "Value ";
    1585                 } else {
    1586                         os << "Posn ";
    1587                 }
    1588                 (*(node->instance)).accept( *this );
    1589                 return node;
    1590         }
    1591 
    15921578        virtual const ast::Type * visit( const ast::TraitInstType * node ) override final {
    15931579                preprint( node );
  • src/AST/Type.hpp

    r41c8312 racb33f15  
    319319using EnumInstType = SueInstType<EnumDecl>;
    320320
    321 class EnumAttrType final : public Type {
    322 public:
    323         readonly<EnumInstType> instance;
    324         EnumAttribute attr;
    325         const Type * accept( Visitor & v ) const override { return v.visit( this ); }
    326         EnumAttrType( const EnumInstType * instance, EnumAttribute attr = EnumAttribute::Posn )
    327                 : instance(instance), attr(attr) {}
    328 
    329         bool match( const ast::EnumAttrType * other) const {
    330                 return instance->base->name == other->instance->base->name && attr == other->attr;
    331         }
    332 private:
    333         EnumAttrType * clone() const override { return new EnumAttrType{ *this }; }
    334         MUTATE_FRIEND
    335 };
    336 
    337321/// An instance of a trait type.
    338322class TraitInstType final : public BaseInstType {
  • src/AST/Visitor.hpp

    r41c8312 racb33f15  
    119119    virtual const ast::Type *             visit( const ast::OneType              * ) = 0;
    120120    virtual const ast::Type *             visit( const ast::GlobalScopeType      * ) = 0;
    121     virtual const ast::Type *             visit( const ast::EnumAttrType         * ) = 0;
    122121    virtual const ast::Designation *      visit( const ast::Designation          * ) = 0;
    123122    virtual const ast::Init *             visit( const ast::SingleInit           * ) = 0;
  • src/CodeGen/GenType.cc

    r41c8312 racb33f15  
    4646        void postvisit( ast::UnionInstType const * type );
    4747        void postvisit( ast::EnumInstType const * type );
    48         void postvisit( ast::EnumAttrType const * type );
    4948        void postvisit( ast::TypeInstType const * type );
    5049        void postvisit( ast::TupleType const * type );
     
    240239}
    241240
    242 void GenType::postvisit( ast::EnumAttrType const * type ) {
    243         postvisit( type->instance );
    244 }
    245 
    246241void GenType::postvisit( ast::TypeInstType const * type ) {
    247242        assertf( !options.genC, "TypeInstType should not reach code generation." );
  • src/Common/CodeLocationTools.cpp

    r41c8312 racb33f15  
    188188    macro(UnionInstType, Type) \
    189189    macro(EnumInstType, Type) \
    190     macro(EnumAttrType, Type) \
    191190    macro(TraitInstType, Type) \
    192191    macro(TypeInstType, Type) \
  • src/ResolvExpr/CandidateFinder.cpp

    r41c8312 racb33f15  
    906906                                }
    907907                                CandidateRef & choice = winners.front();
    908                                 choice->cost.incSafe();
     908                                choice->cost = Cost::unsafe;
    909909                                candidates.emplace_back( std::move(choice) );
    910910                        }
     
    955955
    956956                CandidateFinder funcFinder( context, tenv );
     957                std::string funcName;
    957958                if (auto nameExpr = untypedExpr->func.as<ast::NameExpr>()) {
     959                        funcName = nameExpr->name;
    958960                        auto kind = ast::SymbolTable::getSpecialFunctionKind(nameExpr->name);
    959961                        if (kind != ast::SymbolTable::SpecialFunctionKind::NUMBER_OF_KINDS) {
     
    10191021                CandidateList found;
    10201022                SemanticErrorException errors;
     1023               
    10211024                for ( CandidateRef & func : funcFinder ) {
    10221025                        try {
     
    10931096                        Cost cvtCost = computeApplicationConversionCost( withFunc, symtab );
    10941097
     1098                        if (funcName == "?|?") {
    10951099                        PRINT(
    10961100                                auto appExpr = withFunc->expr.strict_as< ast::ApplicationExpr >();
     
    11081112                                std::cerr << "cost of conversion is:" << cvtCost << std::endl;
    11091113                        )
    1110 
     1114                        }
    11111115                        if ( cvtCost != Cost::infinity ) {
    11121116                                withFunc->cvtCost = cvtCost;
     
    17741778                                                matches.clear();
    17751779                                        }
    1776                                         // ambiguous case, still output candidates to print in error message
    1777                                         if ( cand->cost == minExprCost && thisCost == minCastCost ) {
    1778                                                 auto commonAsEnumAttr = common.as<ast::EnumAttrType>();
    1779                                                 if ( commonAsEnumAttr && commonAsEnumAttr->attr == ast::EnumAttribute::Value ) {
    1780                                                         auto callExpr = new ast::UntypedExpr(
    1781                                                                 cand->expr->location, new ast::NameExpr( cand->expr->location, "valueE"), {cand->expr} );
    1782                                                         CandidateFinder finder( context, env );
    1783                                                         finder.find( callExpr );
    1784                                                         CandidateList winners = findMinCost( finder.candidates );
    1785                                                         if (winners.size() != 1) {
    1786                                                                 SemanticError( callExpr, "Ambiguous expression in valueE..." );
    1787                                                         }
    1788                                                         CandidateRef & choice = winners.front();
    1789                                                         // assert( valueCall->result );
    1790                                                         CandidateRef newCand = std::make_shared<Candidate>(
    1791                                                                 new ast::InitExpr{
    1792                                                                         initExpr->location,
    1793                                                                         // restructureCast( cand->expr, toType ),
    1794                                                                         choice->expr,
    1795                                                                         initAlt.designation },
    1796                                                                 std::move(env), std::move( open ), std::move( need ), cand->cost + thisCost );
    1797                                                                 inferParameters( newCand, matches );
    1798                                                 } else {
    1799                                                         CandidateRef newCand = std::make_shared<Candidate>(
    1800                                                                 new ast::InitExpr{
    1801                                                                         initExpr->location,
    1802                                                                         restructureCast( cand->expr, toType ),
    1803                                                                         initAlt.designation },
    1804                                                                 std::move(env), std::move( open ), std::move( need ), cand->cost + thisCost );
    1805                                                         // currently assertions are always resolved immediately so this should have no effect.
    1806                                                         // if this somehow changes in the future (e.g. delayed by indeterminate return type)
    1807                                                         // we may need to revisit the logic.
    1808                                                         inferParameters( newCand, matches );
    1809                                                 }
    1810                                         }
     1780                                        CandidateRef newCand = std::make_shared<Candidate>(
     1781                                                new ast::InitExpr{
     1782                                                        initExpr->location,
     1783                                                        restructureCast( cand->expr, toType ),
     1784                                                        initAlt.designation },
     1785                                                std::move(env), std::move( open ), std::move( need ), cand->cost + thisCost );
     1786                                        // currently assertions are always resolved immediately so this should have no effect.
     1787                                        // if this somehow changes in the future (e.g. delayed by indeterminate return type)
     1788                                        // we may need to revisit the logic.
     1789                                        inferParameters( newCand, matches );
    18111790                                }
    18121791                        }
  • src/ResolvExpr/CommonType.cc

    r41c8312 racb33f15  
    388388                        const ast::EnumDecl* enumDecl = enumInst->base;
    389389                        if ( !enumDecl->base ) {
    390                                 ast::BasicKind kind = commonTypes[ basic->kind ][ ast::BasicKind::SignedInt ];
    391                                 if (
    392                                         ( ( kind == basic->kind && basic->qualifiers >= type2->qualifiers )
    393                                                 || widen.first )
    394                                         && ( ( kind != basic->kind && basic->qualifiers <= type2->qualifiers )
    395                                                 || widen.second )
    396                                 ) {
    397                                         result = new ast::BasicType{ kind, basic->qualifiers | type2->qualifiers };
    398                                 }
    399                         }
    400                 } else if ( auto type2AsAttr = dynamic_cast< const ast::EnumAttrType * >( type2 ) ) {
    401                         if ( type2AsAttr->attr == ast::EnumAttribute::Posn ) {
    402390                                ast::BasicKind kind = commonTypes[ basic->kind ][ ast::BasicKind::SignedInt ];
    403391                                if (
     
    656644        }
    657645
    658         void postvisit( const ast::EnumAttrType * ) {}
    659 
    660646        void postvisit( const ast::TraitInstType * ) {}
    661647
  • src/ResolvExpr/ConversionCost.cc

    r41c8312 racb33f15  
    279279        if ( const ast::BasicType * dstAsBasic = dynamic_cast< const ast::BasicType * >( dst ) ) {
    280280                conversionCostFromBasicToBasic( basicType, dstAsBasic );
    281         } else if ( dynamic_cast< const ast::EnumAttrType *>(dst) ) {
    282                 static ast::ptr<ast::BasicType> integer = { new ast::BasicType( ast::BasicKind::SignedInt ) };
    283                 cost = costCalc( basicType, integer, srcIsLvalue, symtab, env );
    284         } else if ( auto dstAsEnumInst = dynamic_cast< const ast::EnumInstType * >( dst ) ) {
     281        }       else if ( auto dstAsEnumInst = dynamic_cast< const ast::EnumInstType * >( dst ) ) {
    285282                if ( dstAsEnumInst->base && !dstAsEnumInst->base->isTyped ) {
    286283                        cost = Cost::unsafe;
     
    373370        static ast::ptr<ast::BasicType> integer = { new ast::BasicType( ast::BasicKind::SignedInt ) };
    374371        cost = costCalc( integer, dst, srcIsLvalue, symtab, env );
    375         if ( cost < Cost::unsafe ) {
    376                 cost.incSafe();
    377         }
    378 }
    379 
    380 void ConversionCost::postvisit( const ast::EnumAttrType * src ) {
    381         auto dstAsEnumAttrType = dynamic_cast<const ast::EnumAttrType *>(dst);
    382         assert( src->attr != ast::EnumAttribute::Label );
    383         if ( src->attr == ast::EnumAttribute::Value ) {
    384                 if ( dstAsEnumAttrType && dstAsEnumAttrType->attr == ast::EnumAttribute::Value) {
    385                         cost = costCalc( src->instance, dstAsEnumAttrType->instance, srcIsLvalue, symtab, env );
    386                 } else {
    387                         auto baseType = src->instance->base->base;
    388                         cost = costCalc( baseType, dst, srcIsLvalue, symtab, env );
    389                         if ( cost < Cost::infinity ) {
    390                                 cost.incUnsafe();
    391                         }
    392                 }
    393         } else { // ast::EnumAttribute::Posn
    394                 if ( auto dstBase = dynamic_cast<const ast::EnumInstType *>( dst ) ) {
    395                         cost = costCalc( src->instance, dstBase, srcIsLvalue, symtab, env );
    396                         if ( cost < Cost::unsafe ) cost.incSafe();
    397                 } else {
    398                         static ast::ptr<ast::BasicType> integer = { new ast::BasicType( ast::BasicKind::SignedInt ) };
    399                         cost = costCalc( integer, dst, srcIsLvalue, symtab, env );
    400                         if ( cost < Cost::unsafe ) {
    401                                 cost.incSafe();
    402                         }
    403                 }
    404         }
     372        if ( !inst->base->isTyped ) {
     373                if ( cost < Cost::unsafe ) {
     374                        cost.incSafe();
     375                }
     376                return;
     377        }
     378        cost.incUnsafe();
    405379}
    406380
  • src/ResolvExpr/ConversionCost.h

    r41c8312 racb33f15  
    7272        void postvisit( const ast::ZeroType * zeroType );
    7373        void postvisit( const ast::OneType * oneType );
    74         void postvisit( const ast::EnumAttrType * posType );
    7574private:
    7675        // refactor for code resue
  • src/ResolvExpr/Unify.cc

    r41c8312 racb33f15  
    275275
    276276        void postvisit( const ast::VoidType * vt) {
    277                 result = dynamic_cast< const ast::VoidType * >( type2 )
    278                         || tryToUnifyWithEnumValue(vt, type2, tenv, need, have, open, noWiden());
     277                result = dynamic_cast< const ast::VoidType * >( type2 );
     278                        // || tryToUnifyWithEnumValue(vt, type2, tenv, need, have, open, noWiden());
    279279                ;
    280280        }
     
    284284                        result = basic->kind == basic2->kind;
    285285                }
    286                 result = result || tryToUnifyWithEnumValue(basic, type2, tenv, need, have, open, noWiden());
     286                // result = result || tryToUnifyWithEnumValue(basic, type2, tenv, need, have, open, noWiden());
    287287        }
    288288
     
    293293                                noWiden());
    294294                }
    295                 result = result || tryToUnifyWithEnumValue(pointer, type2, tenv, need, have, open, noWiden());
     295                // result = result || tryToUnifyWithEnumValue(pointer, type2, tenv, need, have, open, noWiden());
    296296        }
    297297
     
    311311
    312312                result = unifyExact(
    313                         array->base, array2->base, tenv, need, have, open, noWiden())
    314                         || tryToUnifyWithEnumValue(array, type2, tenv, need, have, open, noWiden());
     313                        array->base, array2->base, tenv, need, have, open, noWiden());
     314                        // || tryToUnifyWithEnumValue(array, type2, tenv, need, have, open, noWiden());
    315315        }
    316316
     
    404404        }
    405405
    406         bool tryToUnifyWithEnumValue( const ast::Type * type1, const ast::Type * type2, ast::TypeEnvironment & env,
    407                 ast::AssertionSet & need, ast::AssertionSet & have, const ast::OpenVarSet & open,
    408                 WidenMode widen) {
    409                 if ( auto attrType2 = dynamic_cast<const ast::EnumAttrType *>(type2)) {
    410                         if (attrType2->attr == ast::EnumAttribute::Value) {
    411                                 return unifyExact( type1, attrType2->instance->base->base, env, need, have, open,
    412                                         widen);
    413                         } else if (attrType2->attr == ast::EnumAttribute::Posn) {
    414                                 return unifyExact( type1, attrType2->instance, env, need, have, open, widen );
    415                         }
    416                 }
    417                 return false;
    418         }
    419 
    420406public:
    421407        void postvisit( const ast::FunctionType * func ) {
     
    527513        void postvisit( const ast::StructInstType * aggrType ) {
    528514                handleGenericRefType( aggrType, type2 );
    529                 result = result || tryToUnifyWithEnumValue(aggrType, type2, tenv, need, have, open, noWiden());
    530515        }
    531516
    532517        void postvisit( const ast::UnionInstType * aggrType ) {
    533518                handleGenericRefType( aggrType, type2 );
    534                 result = result || tryToUnifyWithEnumValue(aggrType, type2, tenv, need, have, open, noWiden());
    535519        }
    536520
    537521        void postvisit( const ast::EnumInstType * aggrType ) {
    538522                handleRefType( aggrType, type2 );
    539                 result = result || tryToUnifyWithEnumValue(aggrType, type2, tenv, need, have, open, noWiden());
    540         }
    541 
    542         void postvisit( const ast::EnumAttrType * enumAttr ) {
    543                 // Lazy approach for now
    544                 if ( auto otherPos = dynamic_cast< const ast::EnumAttrType *>( type2 ) ) {
    545                         if ( enumAttr->match(otherPos) ) {
    546                                 result = otherPos;
    547                         }
    548                 }
    549523        }
    550524
    551525        void postvisit( const ast::TraitInstType * aggrType ) {
    552526                handleRefType( aggrType, type2 );
    553                 result = result || tryToUnifyWithEnumValue(aggrType, type2, tenv, need, have, open, noWiden());
    554527        }
    555528
     
    560533                        this->result = otherInst;
    561534                }
    562                 result = result || tryToUnifyWithEnumValue(typeInst, type2, tenv, need, have, open, noWiden());
    563535        }
    564536
     
    634606                auto types2 = flatten( flat2 );
    635607
    636                 result = unifyList( types, types2, tenv, need, have, open )
    637                         || tryToUnifyWithEnumValue(tuple, type2, tenv, need, have, open, noWiden());
     608                result = unifyList( types, types2, tenv, need, have, open );
     609                        // || tryToUnifyWithEnumValue(tuple, type2, tenv, need, have, open, noWiden());
    638610        }
    639611
    640612        void postvisit( const ast::VarArgsType * vat) {
    641                 result = dynamic_cast< const ast::VarArgsType * >( type2 )
    642                         || tryToUnifyWithEnumValue(vat, type2, tenv, need, have, open, noWiden());
     613                result = dynamic_cast< const ast::VarArgsType * >( type2 );
     614                        // || tryToUnifyWithEnumValue(vat, type2, tenv, need, have, open, noWiden());
    643615        }
    644616
    645617        void postvisit( const ast::ZeroType * zt) {
    646                 result = dynamic_cast< const ast::ZeroType * >( type2 )
    647                         || tryToUnifyWithEnumValue(zt, type2, tenv, need, have, open, noWiden());
     618                result = dynamic_cast< const ast::ZeroType * >( type2 );
     619                        // || tryToUnifyWithEnumValue(zt, type2, tenv, need, have, open, noWiden());
    648620        }
    649621
    650622        void postvisit( const ast::OneType * ot) {
    651                 result = dynamic_cast< const ast::OneType * >( type2 )
    652                         || tryToUnifyWithEnumValue(ot, type2, tenv, need, have, open, noWiden());
     623                result = dynamic_cast< const ast::OneType * >( type2 );
     624                        // || tryToUnifyWithEnumValue(ot, type2, tenv, need, have, open, noWiden());
    653625        }
    654626};
  • src/SymTab/Mangler.cc

    r41c8312 racb33f15  
    5858        void postvisit( const ast::OneType * oneType );
    5959        void postvisit( const ast::QualifiedType * qualType );
    60         void postvisit( const ast::EnumAttrType * posType );
    6160
    6261        /// The result is the current constructed mangled name.
     
    278277        assertf( decl->kind < ast::TypeDecl::Kind::NUMBER_OF_KINDS, "Unhandled type variable kind: %d", decl->kind );
    279278        mangleName += Encoding::typeVariables[ decl->kind ] + std::to_string( decl->name.length() ) + decl->name;
    280 }
    281 
    282 void Mangler::postvisit( const ast::EnumAttrType * enumAttr ) {
    283         postvisit( enumAttr->instance );
    284         // mangleName += "_pos";
    285         switch ( enumAttr->attr )
    286         {
    287                 case ast::EnumAttribute::Label:
    288                         mangleName += "_label_";
    289                         break;
    290                 case ast::EnumAttribute::Posn:
    291                         mangleName += "_posn_";
    292                         break;
    293                 case ast::EnumAttribute::Value:
    294                         mangleName += "_value_";
    295                         break;
    296         }
    297 
    298279}
    299280
  • src/Validate/ImplementEnumFunc.cpp

    r41c8312 racb33f15  
    7272                ast::DeclWithType* decl = declPtr.get_and_mutate();
    7373                decl->attributes.push_back(new ast::Attribute("unused"));
    74         }
    75 
    76         ast::ObjectDecl* dstParam() const {
    77                 return new ast::ObjectDecl(getLocation(), "_dst",
    78                                            new ast::ReferenceType(new ast::EnumAttrType(
    79                                                ast::deepCopy(instType))));
    80         }
    81 
    82         ast::ObjectDecl* srcParam() const {
    83                 return new ast::ObjectDecl(
    84                         getLocation(), "_src",
    85                         new ast::EnumAttrType(ast::deepCopy(instType)));
    8674        }
    8775
     
    329317}
    330318
    331 inline ast::EnumAttrType * getPosnType( const ast::EnumDecl * decl ) {
    332         return new ast::EnumAttrType(new ast::EnumInstType(decl), ast::EnumAttribute::Posn);
    333 }
    334 
    335319ast::ObjectDecl* EnumAttrFuncGenerator::genAttrArrayProto(
    336320        const ast::EnumAttribute attr, const CodeLocation& location,
     
    360344                        func->location,
    361345                        new ast::VariableExpr( func->location, func->params.front() ),
    362                         new ast::EnumAttrType( new ast::EnumInstType(decl),
    363                                 ast::EnumAttribute::Posn))});
     346                        new ast::BasicType( ast::BasicKind::UnsignedInt ),
     347                        ast::GeneratedFlag::ExplicitCast
     348                )});
    364349        func->stmts = new ast::CompoundStmt(
    365350                func->location, {new ast::ReturnStmt(func->location, untyped)});
     
    370355                func->location,
    371356                new ast::VariableExpr(func->location, func->params.front()),
    372                 new ast::EnumAttrType(new ast::EnumInstType(decl),
    373                                                           ast::EnumAttribute::Posn));
     357                new ast::BasicType( ast::BasicKind::UnsignedInt ),
     358                        ast::GeneratedFlag::ExplicitCast);
    374359        func->stmts = new ast::CompoundStmt(
    375360                func->location, {new ast::ReturnStmt(func->location, castExpr)});
Note: See TracChangeset for help on using the changeset viewer.