Ignore:
Timestamp:
Jan 13, 2023, 4:29:57 PM (15 months ago)
Author:
JiadaL <j82liang@…>
Branches:
ADT, ast-experimental, master
Children:
d61d034
Parents:
8fcf921
Message:

Clean up some code related to Enum codegen

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/CodeGen/CodeGenerator.cc

    r8fcf921 r8bb86ce  
    273273        }
    274274
     275        template<typename pass_type>
     276        inline void genEnumInitializer( PassVisitor<pass_type> * visitor, Type * baseType, std::ostream & output,
     277        Initializer * init, long long * cur_val, Options options) {
     278                auto baseTypeAsBasic = baseType ? dynamic_cast<BasicType *>( baseType ) : nullptr;
     279                if ( init ) { // If value has an explicit initiazatior
     280                        output << " = ";
     281                        output << "(" << genType(baseType, "", options) << ")";
     282                        init->accept( *visitor );
     283                        if ( baseTypeAsBasic && baseTypeAsBasic->isInteger() ) { // if it is an integral type and initilizer offered,
     284                        // need to update the cur_val
     285                                Expression* expr = ((SingleInit *)(init))->value;
     286                                while ( auto temp = dynamic_cast<CastExpr *>(expr) ) { // unwrap introduced cast
     287                                        expr = temp->arg;
     288                                }
     289                                *cur_val = ((ConstantExpr *)expr)->constant.get_ival()+1;
     290                        }
     291                } else if ( baseTypeAsBasic && baseTypeAsBasic->isInteger() ) { // integral implicitly init to cur_val + 1
     292                        output << " = " << "(" << genType(baseType, "", options) << ")";
     293                        output << (*cur_val)++;
     294                }
     295        }
     296
    275297        void CodeGenerator::postvisit( EnumDecl * enumDecl ) {
    276298                extension( enumDecl );
    277299                std::list< Declaration* > &memb = enumDecl->get_members();
    278300                if (enumDecl->base && ! memb.empty()) {
    279                         unsigned long long last_val = -1; // if the first enum value has no explicit initializer,
    280                         // as other
     301                        long long cur_val = 0;
    281302                        for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end();  i++) {
    282303                                ObjectDecl * obj = dynamic_cast< ObjectDecl* >( *i );
     
    284305                                output << "static ";
    285306                                output << genType(enumDecl->base, mangleName( obj ), options);
    286                                 output << " = ";
    287                                 output << "(" << genType(enumDecl->base, "", options) << ")";
    288                                 if ( (BasicType *)(enumDecl->base) && ((BasicType *)(enumDecl->base))->isWholeNumber() ) {
    289                                         if ( obj->get_init() ) {
    290                                                 obj->get_init()->accept( *visitor );
    291                                                 Expression* expr = ((SingleInit *)(obj->init))->value;
    292                                                 while ( auto temp = dynamic_cast<CastExpr *>(expr) ) {
    293                                                         expr = temp->arg;
    294                                                 }
    295                                                 last_val = ((ConstantExpr *)expr)->constant.get_ival();
    296                                         } else {
    297                                                 output << ++last_val;
    298                                         } // if
    299                                 } else {
    300                                         if ( obj->get_init() ) {
    301                                                 obj->get_init()->accept( *visitor );
    302                                         } else {
    303                                                 // Should not reach here!
    304                                         }
    305                                 }
     307                                genEnumInitializer( visitor, enumDecl->base, output, obj->get_init(), &cur_val, options);
    306308                                output << ";" << endl;
    307309                        } // for
Note: See TracChangeset for help on using the changeset viewer.