Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Validate/Autogen.cpp

    r0522ebe rbbf2cb1  
    133133        /// Generates a single struct member operation.
    134134        /// (constructor call, destructor call, assignment call)
    135         // This is managed because it uses another helper that returns a ast::ptr.
    136         ast::ptr<ast::Stmt> makeMemberOp(
     135        const ast::Stmt * makeMemberOp(
    137136                const CodeLocation& location,
    138137                const ast::ObjectDecl * dstParam, const ast::Expr * src,
     
    198197        bool shouldAutogen() const final { return true; }
    199198        void genAttrFuncForward();
     199        void genPosFunctions();
    200200private:
    201201        void genFuncBody( ast::FunctionDecl * decl ) final;
     
    206206        ast::FunctionDecl * genLabelProto() const;
    207207        ast::FunctionDecl * genValueProto() const;
    208         // 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        ast::FunctionDecl * genSuccPredFunc( bool succ );
     215        // ast::FunctionDecl * genPredFunc();
    209216};
    210217
     
    251258        if ( enumDecl->base ) {
    252259                gen.genAttrFuncForward();
     260                gen.genPosFunctions();
    253261        }
    254262        gen.generateAndAppendFunctions( declsToAddAfter );
     
    525533}
    526534
    527 ast::ptr<ast::Stmt> StructFuncGenerator::makeMemberOp(
     535const ast::Stmt * StructFuncGenerator::makeMemberOp(
    528536                const CodeLocation& location, const ast::ObjectDecl * dstParam,
    529537                const ast::Expr * src, const ast::ObjectDecl * field,
     
    540548                )
    541549        );
    542         auto stmt = genImplicitCall(
     550        const ast::Stmt * stmt = genImplicitCall(
    543551                srcParam, dstSelect, location, func->name,
    544552                field, direction
     
    598606                        location, field, new ast::VariableExpr( location, srcParam )
    599607                ) : nullptr;
    600                 ast::ptr<ast::Stmt> stmt =
     608                const ast::Stmt * stmt =
    601609                        makeMemberOp( location, dstParam, srcSelect, field, func, direction );
    602610
    603611                if ( nullptr != stmt ) {
    604                         stmts->kids.push_back( stmt );
     612                        stmts->kids.emplace_back( stmt );
    605613                }
    606614        }
     
    623631        for ( auto param = params.begin() + 1 ; current != end ; ++current ) {
    624632                const ast::ptr<ast::Decl> & member = *current;
    625                 ast::ptr<ast::Stmt> stmt = nullptr;
     633                const ast::Stmt * stmt = nullptr;
    626634                auto field = member.as<ast::ObjectDecl>();
    627635                // Not sure why it could be null.
     
    641649
    642650                if ( nullptr != stmt ) {
    643                         stmts->kids.push_back( stmt );
     651                        stmts->kids.emplace_back( stmt );
    644652                }
    645653        }
     
    791799}
    792800
    793 // ast::FunctionDecl * EnumFuncGenerator::genValueProto2() const {
    794 //      return genProto( "valueE",
    795 //              { new ast::ObjectDecl( getLocation(), "_i", new ast::EnumPosType( new ast::EnumInstType( decl ) ) )},
    796 //              { new ast::ObjectDecl( getLocation(), "_ret", ast::deepCopy( decl->base ) ) } );
    797 // }
     801ast::FunctionDecl * EnumFuncGenerator::genSuccProto() const {
     802        return genProto( "succ",
     803                { new ast::ObjectDecl( getLocation(), "_i", new ast::EnumInstType( decl ) )},
     804                { new ast::ObjectDecl( getLocation(), "_ret", new ast::EnumInstType( decl ))} );
     805}
     806
     807ast::FunctionDecl * EnumFuncGenerator::genPredProto() const {
     808        return genProto( "pred",
     809                { new ast::ObjectDecl( getLocation(), "_i", new ast::EnumInstType( decl ))},
     810                { new ast::ObjectDecl( getLocation(), "_ret", new ast::EnumInstType( decl ))} );
     811}
     812
     813ast::FunctionDecl * EnumFuncGenerator::genSuccPosProto() const {
     814        return genProto( "_successor_",
     815                { new ast::ObjectDecl( getLocation(), "_i",
     816                        new ast::EnumPosType( new ast::EnumInstType( decl ) ) )},
     817                {
     818                        new ast::ObjectDecl( getLocation(), "_ret",
     819                        new ast::EnumPosType( new ast::EnumInstType( decl ) ) )
     820                } );
     821}
     822
     823ast::FunctionDecl * EnumFuncGenerator::genPredPosProto() const {
     824        return genProto( "_predessor_",
     825                { new ast::ObjectDecl( getLocation(), "_i",
     826                        new ast::EnumPosType( new ast::EnumInstType( decl ) ) )},
     827                {
     828                        new ast::ObjectDecl( getLocation(), "_ret",
     829                        new ast::EnumPosType( new ast::EnumInstType( decl ) ) )
     830                } );
     831}
     832
     833ast::FunctionDecl * EnumFuncGenerator::genSuccPredFunc( bool succ ) {
     834        ast::FunctionDecl * decl = succ? genSuccPosProto(): genPredPosProto();
     835        produceForwardDecl( decl );
     836
     837        const CodeLocation& location = getLocation();
     838
     839        auto & params = decl->params;
     840        assert( params.size() == 1 );
     841        auto param = params.front().strict_as<ast::ObjectDecl>();
     842
     843        auto newReturn = new ast::ObjectDecl( location, "_returns",
     844                new ast::BasicType{ ast::BasicType::SignedInt} );
     845       
     846
     847        ast::UntypedExpr * addOneExpr = new ast::UntypedExpr( location,
     848                new ast::NameExpr( location, succ? "?+?": "?-?" )
     849        );
     850        addOneExpr->args.push_back(
     851                new ast::CastExpr( location,
     852                        new ast::VariableExpr( location, param ),
     853                        new ast::BasicType{ ast::BasicType::SignedInt }
     854                )
     855        );
     856        addOneExpr->args.push_back(
     857                ast::ConstantExpr::from_int( location, 1 )
     858        );
     859
     860        ast::UntypedExpr * assignExpr = new ast::UntypedExpr( location,
     861                new ast::NameExpr( location, "?=?" )
     862        );
     863        assignExpr->args.push_back(     
     864                new ast::VariableExpr( location, newReturn )
     865        );
     866        assignExpr->args.push_back(
     867                addOneExpr
     868        );
     869
     870        decl->stmts = new ast::CompoundStmt( location,
     871                {
     872                        new ast::DeclStmt( location, newReturn ),
     873                        new ast::ExprStmt( location, assignExpr ),
     874                        new ast::ReturnStmt( location,
     875                                new ast::VariableExpr( location, newReturn ))
     876                } );
     877       
     878        return decl;
     879}
    798880
    799881void EnumFuncGenerator::genAttrFuncForward() { 
    800882        if ( decl->base ) {
    801                 ast::FunctionDecl *(EnumFuncGenerator::*attrProtos[3])() const = {
     883                ast::FunctionDecl *(EnumFuncGenerator::*attrProtos[5])() const = {
    802884                        &EnumFuncGenerator::genPosProto, &EnumFuncGenerator::genLabelProto,
    803                         &EnumFuncGenerator::genValueProto
    804                         // , &EnumFuncGenerator::genValueProto2
    805                         };
     885                        &EnumFuncGenerator::genValueProto, &EnumFuncGenerator::genSuccProto,
     886                        &EnumFuncGenerator::genPredProto
     887                        // ,&EnumFuncGenerator::genSuccPosProto,
     888                        // &EnumFuncGenerator::genPredPosProto
     889                };
    806890                for ( auto & generator : attrProtos ) {
    807891                        produceForwardDecl( (this->*generator)() );
    808892                }
    809893        }
     894}
     895
     896void EnumFuncGenerator::genPosFunctions() {
     897        if ( decl->base ) {
     898                ast::FunctionDecl * succ = genSuccPredFunc( true );
     899                ast::FunctionDecl * pred = genSuccPredFunc( false );
     900                produceDecl( succ );
     901                produceDecl( pred );
     902        }
     903
    810904}
    811905
Note: See TracChangeset for help on using the changeset viewer.