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

Fixed the problem when enum use another enumerator as initializer

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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.