Changeset 4a72fef


Ignore:
Timestamp:
Apr 4, 2024, 11:33:08 AM (4 weeks ago)
Author:
Andrew Beach <ajbeach@…>
Branches:
master
Children:
1cfe640
Parents:
2d82999
Message:

General clean-up in the parser. Removed the commented DeclarationNode::name and added some helper functions to remove duplicate code.

Location:
src/Parser
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • src/Parser/DeclarationNode.cc

    r2d82999 r4a72fef  
    5353        linkage( ::linkage ) {
    5454
    55 //      variable.name = nullptr;
    5655        variable.tyClass = ast::TypeDecl::NUMBER_OF_KINDS;
    5756        variable.assertions = nullptr;
     
    6564        delete name;
    6665
    67 //      delete variable.name;
    6866        delete variable.assertions;
    6967        delete variable.initializer;
    7068
    71 //      delete type;
     69        delete type;
    7270        delete bitfieldWidth;
    7371
     
    10098        newnode->error = error;
    10199
    102 //      newnode->variable.name = variable.name ? new string( *variable.name ) : nullptr;
    103100        newnode->variable.tyClass = variable.tyClass;
    104101        newnode->variable.assertions = maybeCopy( variable.assertions );
     
    584581DeclarationNode * DeclarationNode::addAssertions( DeclarationNode * assertions ) {
    585582        if ( variable.tyClass != ast::TypeDecl::NUMBER_OF_KINDS ) {
    586                 if ( variable.assertions ) {
    587                         variable.assertions->set_last( assertions );
    588                 } else {
    589                         variable.assertions = assertions;
    590                 } // if
     583                extend( variable.assertions, assertions );
    591584                return this;
    592585        } // if
    593586
    594587        assert( type );
    595         switch ( type->kind ) {
    596         case TypeData::Symbolic:
    597                 if ( type->symbolic.assertions ) {
    598                         type->symbolic.assertions->set_last( assertions );
    599                 } else {
    600                         type->symbolic.assertions = assertions;
    601                 } // if
    602                 break;
    603         default:
    604                 assert( false );
    605         } // switch
     588        assert( TypeData::Symbolic == type->kind );
     589        extend( type->symbolic.assertions, assertions );
    606590
    607591        return this;
     
    688672
    689673DeclarationNode * DeclarationNode::addNewPointer( DeclarationNode * p ) {
    690         if ( p ) {
    691                 assert( p->type->kind == TypeData::Pointer || p->type->kind == TypeData::Reference );
    692                 if ( type ) {
    693                         p->type->base = makeNewBase( type );
    694                         type = nullptr;
    695                 } // if
    696                 delete this;
    697                 return p;
    698         } else {
    699                 return this;
    700         } // if
     674        if ( !p ) return this;
     675        assert( p->type->kind == TypeData::Pointer || p->type->kind == TypeData::Reference );
     676        if ( type ) {
     677                p->type->base = makeNewBase( type );
     678                type = nullptr;
     679        } // if
     680        delete this;
     681        return p;
    701682}
    702683
    703684DeclarationNode * DeclarationNode::addNewArray( DeclarationNode * a ) {
    704         if ( ! a ) return this;
     685        if ( !a ) return this;
    705686        assert( a->type->kind == TypeData::Array );
    706687        if ( type ) {
     
    774755                        if ( ret->kind == TypeData::Aggregate ) {
    775756                                newnode->attributes.swap( ret->aggregate.attributes );
    776                         } // if 
     757                        } // if
    777758                        return newnode;
    778759                } // if
  • src/Parser/DeclarationNode.h

    r2d82999 r4a72fef  
    9999
    100100        struct Variable_t {
    101 //              const std::string * name;
    102101                ast::TypeDecl::Kind tyClass;
    103102                DeclarationNode * assertions;
  • src/Parser/ParseNode.h

    r2d82999 r4a72fef  
    8282};
    8383
     84template<typename Node>
     85void extend( Node *& list, Node * value ) {
     86        if ( list ) {
     87                extend( list->next, value );
     88        } else {
     89                list = value;
     90        }
     91}
     92
    8493// Must harmonize with OperName.
    8594enum class OperKinds {
  • src/Parser/TypeData.cc

    r2d82999 r4a72fef  
    475475
    476476
     477// Wrap an aggregate up in an instance. Takes and gives ownership.
     478static TypeData * makeInstance( TypeData * type ) {
     479        assert( TypeData::Aggregate == type->kind );
     480        TypeData * out = new TypeData( TypeData::AggregateInst );
     481        out->aggInst.aggregate = type;
     482        out->aggInst.params = maybeCopy( type->aggregate.actuals );
     483        out->aggInst.hoistType = type->aggregate.body;
     484        out->qualifiers |= type->qualifiers;
     485        return out;
     486}
     487
     488
    477489TypeData * build_type_qualifier( ast::CV::Qualifiers tq ) {
    478490        TypeData * type = new TypeData;
     
    566578TypeData * addQualifiers( TypeData * ltype, TypeData * rtype ) {
    567579        if ( ltype->forall ) {
    568                 if ( rtype->forall ) {
    569                         rtype->forall->set_last( ltype->forall );
    570                 } else if ( TypeData::Aggregate != rtype->kind ) {
    571                         rtype->forall = ltype->forall;
    572                 } else if ( rtype->aggregate.params ) {
    573                         rtype->aggregate.params->set_last( ltype->forall );
     580                if ( rtype->forall || TypeData::Aggregate != rtype->kind ) {
     581                        extend( rtype->forall, ltype->forall );
    574582                } else {
    575                         rtype->aggregate.params = ltype->forall;
     583                        extend( rtype->aggregate.params, ltype->forall );
    576584                }
    577585                ltype->forall = nullptr;
     
    585593static void addTypeToType( TypeData *& dst, TypeData *& src ) {
    586594        if ( src->forall && dst->kind == TypeData::Function ) {
    587                 if ( dst->forall ) {
    588                         dst->forall->set_last( src->forall );
    589                 } else {
    590                         dst->forall = src->forall;
    591                 } // if
     595                extend( dst->forall, src->forall );
    592596                src->forall = nullptr;
    593597        } // if
     
    641645                break;
    642646        default:
    643                 switch ( src->kind ) {
    644                 case TypeData::Aggregate:
    645                         dst->base = new TypeData( TypeData::AggregateInst );
    646                         dst->base->aggInst.aggregate = src;
    647                         if ( src->kind == TypeData::Aggregate ) {
    648                                 dst->base->aggInst.params = maybeCopy( src->aggregate.actuals );
    649                         } // if
    650                         dst->base->qualifiers |= src->qualifiers;
    651                         src = nullptr;
    652                         break;
    653                 default:
    654                         if ( dst->forall ) {
    655                                 dst->forall->set_last( src->forall );
    656                         } else {
    657                                 dst->forall = src->forall;
    658                         } // if
     647                if ( TypeData::Aggregate == src->kind ) {
     648                        dst->base = makeInstance( src );
     649                } else {
     650                        extend( dst->forall, src->forall );
    659651                        src->forall = nullptr;
    660652                        dst->base = src;
    661                         src = nullptr;
    662                 } // switch
     653                }
     654                src = nullptr;
    663655        } // switch
    664656}
     
    668660        if ( rtype ) {
    669661                addTypeToType( rtype, ltype );
    670                 return rtype;
     662        } else if ( ltype->kind == TypeData::Aggregate ) {
     663                // Hide type information aggregate instances.
     664                rtype = makeInstance( ltype );
     665                rtype->aggInst.aggregate->aggregate.attributes.swap( attributes );
    671666        } else {
    672                 if ( ltype->kind == TypeData::Aggregate ) {
    673                         // Hide type information aggregate instances.
    674                         rtype = new TypeData( TypeData::AggregateInst );
    675                         rtype->aggInst.aggregate = ltype;
    676                         rtype->aggInst.aggregate->aggregate.attributes.swap( attributes ); // change ownership
    677                         rtype->aggInst.hoistType = ltype->aggregate.body;
    678                         rtype->aggInst.params = maybeCopy( ltype->aggregate.actuals );
    679                         rtype->qualifiers |= ltype->qualifiers;
    680                 } else {
    681                         rtype = ltype;
    682                 } // if
    683                 return rtype;
     667                rtype = ltype;
    684668        } // if
     669        return rtype;
    685670}
    686671
     
    713698
    714699TypeData * makeNewBase( TypeData * type ) {
    715         switch ( type->kind ) {
    716         case TypeData::Aggregate: {
    717                 TypeData * out = new TypeData( TypeData::AggregateInst );
    718                 out->aggInst.aggregate = type;
    719                 if ( TypeData::Aggregate == type->kind ) {
    720                         out->aggInst.params = maybeCopy( type->aggregate.actuals );
    721                 }
    722                 out->qualifiers |= type->qualifiers;
    723                 return out;
    724         }
    725         default:
    726                 return type;
    727         } // switch
     700        return ( TypeData::Aggregate == type->kind ) ? makeInstance( type ) : type;
    728701}
    729702
Note: See TracChangeset for help on using the changeset viewer.