Ignore:
Timestamp:
Jun 12, 2023, 2:45:32 PM (2 years ago)
Author:
Fangren Yu <f37yu@…>
Branches:
ast-experimental, master
Children:
62d62db
Parents:
34b4268 (diff), 251ce80 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge branch 'master' into ast-experimental

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/CodeGen/CodeGenerator.cc

    r34b4268 r24d6572  
    1717#include <cassert>                   // for assert, assertf
    1818#include <list>                      // for _List_iterator, list, list<>::it...
     19#include <sstream>                   // for stringstream
    1920
    2021#include "AST/Decl.hpp"              // for DeclWithType
    2122#include "Common/UniqueName.h"       // for UniqueName
    22 #include "Common/utility.h"          // for CodeLocation, toString
    2323#include "GenType.h"                 // for genType
    2424#include "InitTweak/InitTweak.h"     // for getPointerBase
     
    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 );
    283304                                assert( obj );
    284305                                output << "static ";
    285                                 output << genType(enumDecl->base, "", options) << " const ";
    286                                 output << mangleName( obj ) << " ";
    287                                 output << " = ";
    288                                 output << "(" << genType(enumDecl->base, "", options) << ")";
    289                                 if ( (BasicType *)(enumDecl->base) && ((BasicType *)(enumDecl->base))->isWholeNumber() ) {
    290                                         if ( obj->get_init() ) {
    291                                                 obj->get_init()->accept( *visitor );
    292                                                 Expression* expr = ((SingleInit *)(obj->init))->value;
    293                                                 while ( auto temp = dynamic_cast<CastExpr *>(expr) ) {
    294                                                         expr = temp->arg;
    295                                                 }
    296                                                 last_val = ((ConstantExpr *)expr)->constant.get_ival();
    297                                         } else {
    298                                                 output << ++last_val;
    299                                         } // if
    300                                 } else {
    301                                         if ( obj->get_init() ) {
    302                                                 obj->get_init()->accept( *visitor );
    303                                         } else {
    304                                                 // Should not reach here!
    305                                         }
    306                                 }
     306                                output << genType(enumDecl->base, mangleName( obj ), options);
     307                                genEnumInitializer( visitor, enumDecl->base, output, obj->get_init(), &cur_val, options);
    307308                                output << ";" << endl;
    308309                        } // for
Note: See TracChangeset for help on using the changeset viewer.