Changeset 00eaeb8


Ignore:
Timestamp:
Mar 1, 2024, 4:32:35 PM (3 months ago)
Author:
JiadaL <j82liang@…>
Branches:
master
Children:
bbf2cb1
Parents:
f1149ac
Message:

Add prototype of succ function

Location:
src
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • src/ResolvExpr/ConversionCost.cc

    rf1149ac r00eaeb8  
    288288            cost = Cost::unsafe;
    289289                }
     290        } else if ( dynamic_cast< const ast::EnumPosType *>(dst) ) {
     291                static ast::ptr<ast::BasicType> integer = { new ast::BasicType( ast::BasicType::SignedInt ) };
     292                cost = costCalc( basicType, integer, srcIsLvalue, symtab, env );
    290293        }
    291294}
     
    366369        static ast::ptr<ast::BasicType> integer = { new ast::BasicType( ast::BasicType::SignedInt ) };
    367370        cost = costCalc( integer, dst, srcIsLvalue, symtab, env );
    368         // }
    369371        if ( cost < Cost::unsafe ) {
    370372                cost.incSafe();
     
    373375
    374376void ConversionCost::postvisit( const ast::EnumPosType * ) {
    375         static ast::ptr<ast::BasicType> integer = { new ast::BasicType( ast::BasicType::SignedInt ) };
    376         cost = costCalc( integer, dst, srcIsLvalue, symtab, env );
    377         if ( cost < Cost::unsafe ) {
    378                 cost.incSafe();
    379         }
     377        if ( dynamic_cast<const ast::EnumPosType *>( dst ) ) {
     378                // Tempoarary
     379                cost = Cost::zero;
     380        } else {
     381                static ast::ptr<ast::BasicType> integer = { new ast::BasicType( ast::BasicType::SignedInt ) };
     382                cost = costCalc( integer, dst, srcIsLvalue, symtab, env );
     383                if ( cost < Cost::unsafe ) {
     384                        cost.incSafe();
     385                }
     386        }
     387
    380388}
    381389
  • src/Validate/Autogen.cpp

    rf1149ac r00eaeb8  
    197197        bool shouldAutogen() const final { return true; }
    198198        void genAttrFuncForward();
     199        void genPosFunctions();
    199200private:
    200201        void genFuncBody( ast::FunctionDecl * decl ) final;
     
    205206        ast::FunctionDecl * genLabelProto() const;
    206207        ast::FunctionDecl * genValueProto() const;
    207         // ast::FunctionDecl * genValueProto2() const;
     208        ast::FunctionDecl * genSuccProto() const;
     209        ast::FunctionDecl * genPredProto() const;
     210
     211        ast::FunctionDecl * genSuccPosProto() const;
     212        ast::FunctionDecl * genPredPosProto() const;
     213
     214        void genSuccFunc(ast::FunctionDecl *);
    208215};
    209216
     
    250257        if ( enumDecl->base ) {
    251258                gen.genAttrFuncForward();
     259                gen.genPosFunctions();
    252260        }
    253261        gen.generateAndAppendFunctions( declsToAddAfter );
     
    790798}
    791799
    792 // ast::FunctionDecl * EnumFuncGenerator::genValueProto2() const {
    793 //      return genProto( "valueE",
    794 //              { new ast::ObjectDecl( getLocation(), "_i", new ast::EnumPosType( new ast::EnumInstType( decl ) ) )},
    795 //              { new ast::ObjectDecl( getLocation(), "_ret", ast::deepCopy( decl->base ) ) } );
    796 // }
     800ast::FunctionDecl * EnumFuncGenerator::genSuccProto() const {
     801        return genProto( "succ",
     802                { new ast::ObjectDecl( getLocation(), "_i", new ast::EnumInstType( decl ) )},
     803                { new ast::ObjectDecl( getLocation(), "_ret", new ast::EnumInstType( decl ))} );
     804}
     805
     806ast::FunctionDecl * EnumFuncGenerator::genPredProto() const {
     807        return genProto( "pred",
     808                { new ast::ObjectDecl( getLocation(), "_i", new ast::EnumInstType( decl ))},
     809                { new ast::ObjectDecl( getLocation(), "_ret", new ast::EnumInstType( decl ))} );
     810}
     811
     812ast::FunctionDecl * EnumFuncGenerator::genSuccPosProto() const {
     813        return genProto( "succ",
     814                { new ast::ObjectDecl( getLocation(), "_i",
     815                        new ast::EnumPosType( new ast::EnumInstType( decl ) ) )},
     816                {
     817                        new ast::ObjectDecl( getLocation(), "_ret",
     818                        new ast::EnumPosType( new ast::EnumInstType( decl ) ) )
     819                } );
     820}
     821
     822ast::FunctionDecl * EnumFuncGenerator::genPredPosProto() const {
     823        return genProto( "pred",
     824                { new ast::ObjectDecl( getLocation(), "_i",
     825                        new ast::EnumPosType( new ast::EnumInstType( decl ) ) )},
     826                {
     827                        new ast::ObjectDecl( getLocation(), "_ret",
     828                        new ast::EnumPosType( new ast::EnumInstType( decl ) ) )
     829                } );
     830}
     831
     832void EnumFuncGenerator::genSuccFunc(ast::FunctionDecl * succDecl) {
     833        const CodeLocation& location = getLocation();
     834
     835        auto & params = succDecl->params;
     836        assert( params.size() == 1 );
     837        auto param = params.front().strict_as<ast::ObjectDecl>();
     838
     839        // auto & returns = succDecl->returns;
     840        // assert( returns.size() == 1 );
     841        // auto oldRet = returns.front().strict_as<ast::ObjectDecl>();
     842
     843        // auto param = new ast::ObjectDecl( getLocation(), "_i",
     844        //              new ast::EnumPosType( new ast::EnumInstType( decl ) ) );
     845
     846        auto newReturn = new ast::ObjectDecl( location, "_returns",
     847                new ast::BasicType{ ast::BasicType::SignedInt} );
     848       
     849
     850        ast::UntypedExpr * addOneExpr = new ast::UntypedExpr( location,
     851                new ast::NameExpr( location, "?+?" )
     852        );
     853        addOneExpr->args.push_back(
     854                new ast::CastExpr( location,
     855                        new ast::VariableExpr( location, param ),
     856                        new ast::BasicType{ ast::BasicType::SignedInt }
     857                )
     858        );
     859        addOneExpr->args.push_back(
     860                ast::ConstantExpr::from_int( location, 1 )
     861        );
     862
     863        ast::UntypedExpr * assignExpr = new ast::UntypedExpr( location,
     864                new ast::NameExpr( location, "?=?" )
     865        );
     866        assignExpr->args.push_back(     
     867                new ast::VariableExpr( location, newReturn )
     868        );
     869        assignExpr->args.push_back(
     870                addOneExpr
     871        );
     872
     873        succDecl->stmts = new ast::CompoundStmt( location,
     874                {
     875                        new ast::DeclStmt( location, newReturn ),
     876                        new ast::ExprStmt( location, assignExpr ),
     877                        new ast::ReturnStmt( location,
     878                                new ast::VariableExpr( location, newReturn ))
     879                } );
     880}
    797881
    798882void EnumFuncGenerator::genAttrFuncForward() { 
    799883        if ( decl->base ) {
    800                 ast::FunctionDecl *(EnumFuncGenerator::*attrProtos[3])() const = {
     884                ast::FunctionDecl *(EnumFuncGenerator::*attrProtos[5])() const = {
    801885                        &EnumFuncGenerator::genPosProto, &EnumFuncGenerator::genLabelProto,
    802                         &EnumFuncGenerator::genValueProto
    803                         // , &EnumFuncGenerator::genValueProto2
    804                         };
     886                        &EnumFuncGenerator::genValueProto, &EnumFuncGenerator::genSuccProto,
     887                        &EnumFuncGenerator::genPredProto
     888                };
    805889                for ( auto & generator : attrProtos ) {
    806890                        produceForwardDecl( (this->*generator)() );
    807891                }
    808892        }
     893}
     894
     895void EnumFuncGenerator::genPosFunctions() {
     896        if ( decl->base ) {
     897                ast::FunctionDecl * decl = genSuccPosProto();
     898                produceForwardDecl( decl );
     899                genSuccFunc (decl );
     900                produceDecl( decl );
     901        }
     902
    809903}
    810904
Note: See TracChangeset for help on using the changeset viewer.