Changeset 30d91e4


Ignore:
Timestamp:
Apr 14, 2022, 2:59:16 PM (2 years ago)
Author:
JiadaL <j82liang@…>
Branches:
ADT, ast-experimental, enum, master, pthread-emulation, qualifiedEnum
Children:
365c8dcb
Parents:
d8c4fab
Message:

Change the code gen for enum value. Hope it fixes the gcc compatibility problem

Files:
3 edited

Legend:

Unmodified
Added
Removed
  • src/CodeGen/CodeGenerator.cc

    rd8c4fab r30d91e4  
    276276                std::list< Declaration* > &memb = enumDecl->get_members();
    277277                if (enumDecl->base && ! memb.empty()) {
    278                         ObjectDecl * last = nullptr;
     278                        unsigned long long last_val = -1;
    279279                        for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end();  i++) {
    280280                                ObjectDecl * obj = dynamic_cast< ObjectDecl* >( *i );
     
    284284                                output << mangleName( obj ) << " ";
    285285                                output << " = ";
    286                                 output << "(" << genType(enumDecl->base, "", options) << ")";;
    287                                 if ( obj->get_init() ) {
    288                                         obj->get_init()->accept( *visitor );
     286                                output << "(" << genType(enumDecl->base, "", options) << ")";
     287                                if ( (BasicType *)(enumDecl->base) && ((BasicType *)(enumDecl->base))->isWholeNumber() ) {
     288                                        if ( obj->get_init() ) {
     289                                                obj->get_init()->accept( *visitor );
     290                                                last_val = ((ConstantExpr *)(((SingleInit *)(obj->init))->value))->constant.get_ival();
     291                                        } else {
     292                                                output << ++last_val;
     293                                        } // if
    289294                                } else {
    290                                         if (last == nullptr) {
    291                                                 output << 0;
     295                                        if ( obj->get_init() ) {
     296                                                obj->get_init()->accept( *visitor );
    292297                                        } else {
    293                                                 output << mangleName(last) << " + 1";
     298                                                // Should not reach here!
    294299                                        }
    295                                 } // if—
     300                                }
    296301                                output << ";" << endl;
    297                                 last = obj;
    298302                        } // for
    299303                } else {
  • tests/enum_tests/.expect/typedIntEnum.txt

    rd8c4fab r30d91e4  
    1 zero: 0, one: 1
     10
     21
     31000
     41001
     52000
     62001
     72002
  • tests/enum_tests/typedIntEnum.cfa

    rd8c4fab r30d91e4  
    33enum(int) IntEnum {
    44    zero,
    5     one
     5    one,
     6    thousand = 1000,
     7    thousand_one,
     8    two_thousand = 2000,
     9    two_thousand_one,
     10    two_thousand_two
    611};
    712
    813int main() {
    9     printf("zero: %d, one: %d\n", zero, one);
     14    printf("%d\n", zero);
     15    printf("%d\n", one);
     16    printf("%d\n", thousand);
     17    printf("%d\n", thousand_one);
     18    printf("%d\n", two_thousand);
     19    printf("%d\n", two_thousand_one);
     20    printf("%d\n", two_thousand_two);
    1021    return 0;
    1122}
Note: See TracChangeset for help on using the changeset viewer.