Changeset bb336a6 for src/Validate


Ignore:
Timestamp:
Jul 10, 2024, 3:39:26 AM (6 weeks ago)
Author:
JiadaL <j82liang@…>
Branches:
master
Children:
dbff8ec
Parents:
550afde2
Message:

Fixed the problem when enum use another enumerator as initializer

Location:
src/Validate
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • src/Validate/Autogen.cpp

    r550afde2 rbb336a6  
    8888
    8989        // Internal helpers:
    90         void genStandardFuncs();
     90        virtual void genStandardFuncs();
    9191        void produceDecl( const ast::FunctionDecl * decl );
    9292        void produceForwardDecl( const ast::FunctionDecl * decl );
     
    196196
    197197        bool shouldAutogen() const final { return true; }
     198        void generateAndPrependDecls( std::list<ast::ptr<ast::Decl>> & );
     199        void genForwards();
    198200private:
    199201        void genFuncBody( ast::FunctionDecl * decl ) final;
    200202        void genFieldCtors() final;
    201203        const ast::Decl * getDecl() const final { return decl; }
     204protected:
     205        void genStandardFuncs() override;
    202206};
    203207
     
    242246        enumInst.base = enumDecl;
    243247        EnumFuncGenerator gen( enumDecl, &enumInst, functionNesting );
    244         gen.generateAndAppendFunctions( declsToAddAfter );
     248        gen.generateAndPrependDecls( declsToAddBefore );
     249
     250        EnumFuncGenerator gen2( enumDecl, &enumInst, functionNesting );
     251        gen2.generateAndAppendFunctions( declsToAddAfter );
    245252}
    246253
     
    709716}
    710717
     718void EnumFuncGenerator::generateAndPrependDecls( std::list<ast::ptr<ast::Decl>> & decls ) {
     719        genForwards();
     720        decls.splice( decls.end(), forwards );
     721}
     722
     723void EnumFuncGenerator::genForwards() {
     724        forwards.push_back( ast::asForward(decl) );
     725
     726        ast::FunctionDecl *(FuncGenerator::*standardProtos[4])() const = {
     727                &EnumFuncGenerator::genCtorProto, &EnumFuncGenerator::genCopyProto,
     728                &EnumFuncGenerator::genDtorProto, &EnumFuncGenerator::genAssignProto };
     729
     730        for ( auto & generator: standardProtos) {
     731                ast::FunctionDecl * decl = (this->*generator)();
     732                produceForwardDecl( decl );
     733        }
     734}
     735
     736void EnumFuncGenerator::genStandardFuncs() {
     737        // do everything FuncGenerator does except not make ForwardDecls
     738        ast::FunctionDecl *(FuncGenerator::*standardProtos[4])() const = {
     739                &EnumFuncGenerator::genCtorProto, &EnumFuncGenerator::genCopyProto,
     740                &EnumFuncGenerator::genDtorProto, &EnumFuncGenerator::genAssignProto };
     741
     742        for ( auto & generator : standardProtos ) {
     743                ast::FunctionDecl * decl = (this->*generator)();
     744                // produceForwardDecl( decl ); Done in genForwards
     745                genFuncBody( decl );
     746                if ( CodeGen::isAssignment( decl->name ) ) {
     747                        appendReturnThis( decl );
     748                }
     749                produceDecl( decl );
     750        }
     751}
     752
    711753void EnumFuncGenerator::genFieldCtors() {
    712754        // Enumerations to not have field constructors.
  • src/Validate/ImplementEnumFunc.cpp

    r550afde2 rbb336a6  
    104104                auto memAsObjectDecl = mem.as<ast::ObjectDecl>();
    105105                assert(memAsObjectDecl);
    106                 if (memAsObjectDecl->init) {
     106                if (auto& init = memAsObjectDecl->init) {
     107                        auto singleInit = init.strict_as<ast::SingleInit>();
     108                        auto nameExpr = singleInit->value.as<ast::NameExpr>();
     109                        if (nameExpr) {
     110                                auto name = nameExpr->name;
     111                                if (auto it = std::find_if(decl->members.begin(), decl->members.end(),
     112                                        [name](ast::ptr<ast::Decl> mem_decl) {
     113                                                return (mem_decl->name == name);
     114                                        }); it != std::end(decl->members)
     115                                ) {
     116                                        // ast::SingleInit* newInit = new ast::SingleInit(
     117                                        //      init->location, new ast::CastExpr( nameExpr->location,
     118                                        //      nameExpr, new ast::BasicType( ast::BasicKind::UnsignedInt ), ast::ExplicitCast )
     119                                        // );
     120                                        // auto targetInit = (*it).strict_as<ast::ObjectDecl>()->init;
     121                                        // inits.emplace_back( targetInit );
     122                                        auto index = std::distance( decl->members.begin(), it );
     123                                        auto targetInit = inits.at(index).strict_as<ast::SingleInit>();
     124                                        auto targetExpr = targetInit->value;
     125                                        inits.push_back( new ast::SingleInit( targetExpr->location, targetExpr ) );
     126                                        continue;
     127                                }
     128                        }
     129
    107130                        inits.emplace_back(memAsObjectDecl->init);
    108131                } else {
     
    406429void EnumAttrFuncGenerator::genTypedEnumFunction(const ast::EnumAttribute attr) {
    407430        if (attr == ast::EnumAttribute::Value) {
    408                 if (decl->base) {
     431                if (decl->isTyped()) {
    409432                        // TypedEnum's backing arrays
    410433                        std::vector<ast::ptr<ast::Init>> inits = genValueInit();
     
    441464
    442465void EnumAttrFuncGenerator::genTypedEnumFuncs() {
    443         genTypedEnumFunction(ast::EnumAttribute::Value);
    444         genTypedEnumFunction(ast::EnumAttribute::Label);
    445         genTypedEnumFunction(ast::EnumAttribute::Posn);
     466        // genTypedEnumFunction(ast::EnumAttribute::Value);
     467        // genTypedEnumFunction(ast::EnumAttribute::Label);
     468        // genTypedEnumFunction(ast::EnumAttribute::Posn);
    446469}
    447470
     
    456479        std::list<ast::ptr<ast::Decl>>& decls) {
    457480        // Generate the functions (they go into forwards and definitions).
    458         genTypeNameFunc();
     481        // genTypeNameFunc();
    459482        genTypedEnumFuncs();
    460         genSerialTraitFuncs();
    461         genBoundedFunctions();
     483        // genSerialTraitFuncs();
     484        // genBoundedFunctions();
    462485        // Now export the lists contents.
    463486        decls.splice(decls.end(), forwards);
     
    483506        enumInst.base = enumDecl;
    484507        EnumAttrFuncGenerator gen(enumDecl, &enumInst, functionNesting);
    485         gen.generateAndAppendFunctions(declsToAddAfter);
     508        // gen.generateAndAppendFunctions(declsToAddAfter);
    486509}
    487510
Note: See TracChangeset for help on using the changeset viewer.