Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Parser/DeclarationNode.cc

    rac71a86 r7bf7fb9  
    1010// Created On       : Sat May 16 12:34:05 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Thu Aug 18 23:48:23 2016
    13 // Update Count     : 182
     12// Last Modified On : Sun Aug  7 08:01:55 2016
     13// Update Count     : 165
    1414//
    1515
     
    2525#include "SynTree/Expression.h"
    2626
     27#include "Parser.h"
    2728#include "TypedefTable.h"
    2829extern TypedefTable typedefTable;
     
    4142UniqueName DeclarationNode::anonymous( "__anonymous" );
    4243
    43 extern LinkageSpec::Spec linkage;                                               // defined in parser.yy
     44extern LinkageSpec::Type linkage;                                               // defined in parser.yy
    4445
    4546DeclarationNode *DeclarationNode::clone() const {
     
    4748        newnode->type = maybeClone( type );
    4849        newnode->name = name;
    49         newnode->storageClass = storageClass;
    50         newnode->isInline = isInline;
    51         newnode->isNoreturn = isNoreturn;
     50        newnode->storageClasses = storageClasses;
     51//PAB   newnode->bitfieldWidth = maybeClone( bitfieldWidth );
    5252        newnode->bitfieldWidth = bitfieldWidth;
    5353        newnode->hasEllipsis = hasEllipsis;
    5454        newnode->initializer = initializer;
    55         newnode->set_next( maybeClone( get_next() ) );
     55        newnode->next = maybeClone( next );
    5656        newnode->linkage = linkage;
    5757        return newnode;
    5858} // DeclarationNode::clone
    5959
    60 DeclarationNode::DeclarationNode()
    61         : type( 0 )
    62         , storageClass( NoStorageClass )
    63         , isInline( false )
    64         , isNoreturn( false )
    65         , bitfieldWidth( 0 )
    66         , initializer( 0 )
    67         , hasEllipsis( false )
    68         , linkage( ::linkage )
    69         , extension( false )
    70         , error() {
     60DeclarationNode::DeclarationNode() : type( 0 ), bitfieldWidth( 0 ), initializer( 0 ), hasEllipsis( false ), linkage( ::linkage ) {
    7161}
    7262
     
    9383        } // if
    9484
    95         if(storageClass != NoStorageClass) os << DeclarationNode::storageName[storageClass] << ' ';
    96         if(isInline) os << DeclarationNode::storageName[Inline] << ' ';
    97         if(isNoreturn) os << DeclarationNode::storageName[Noreturn] << ' ';
     85        printEnums( storageClasses.begin(), storageClasses.end(), DeclarationNode::storageName, os );
    9886        if ( type ) {
    9987                type->print( os, indent );
     
    156144DeclarationNode *DeclarationNode::newStorageClass( DeclarationNode::StorageClass sc ) {
    157145        DeclarationNode *newnode = new DeclarationNode;
    158         switch (sc) {
    159                 case Inline: newnode->isInline = true; break;
    160                 case Noreturn: newnode->isNoreturn = true; break;
    161                 default: newnode->storageClass = sc; break;
    162         }
     146        newnode->storageClasses.push_back( sc );
    163147        return newnode;
    164148} // DeclarationNode::newStorageClass
     
    300284        newnode->type->array->dimension = size;
    301285        newnode->type->array->isStatic = isStatic;
    302         if ( newnode->type->array->dimension == 0 || newnode->type->array->dimension->isExpressionType<ConstantExpr *>() ) {
     286        if ( newnode->type->array->dimension == 0 || dynamic_cast<ConstantNode *>( newnode->type->array->dimension ) ) {
    303287                newnode->type->array->isVarLen = false;
    304288        } else {
     
    376360DeclarationNode *DeclarationNode::addQualifiers( DeclarationNode *q ) {
    377361        if ( q ) {
    378                 copyStorageClasses(q);
     362                storageClasses.splice( storageClasses.end(), q->storageClasses );
    379363                if ( q->type ) {
    380364                        if ( ! type ) {
     
    403387
    404388DeclarationNode *DeclarationNode::copyStorageClasses( DeclarationNode *q ) {
    405         isInline = isInline || q->isInline;
    406         isNoreturn = isNoreturn || q->isNoreturn;
    407         if(storageClass == NoStorageClass) {
    408                 storageClass = q->storageClass;
    409         }
    410         else if (q->storageClass != NoStorageClass) {
    411                 q->error = "invalid combination of storage classes in declaration of ";
    412         }
    413         if(error.empty()) error = q->error;
     389        storageClasses = q->storageClasses;
    414390        return this;
    415391}
     
    471447DeclarationNode *DeclarationNode::addType( DeclarationNode *o ) {
    472448        if ( o ) {
    473                 copyStorageClasses( o );
     449                storageClasses.splice( storageClasses.end(), o->storageClasses );
    474450                if ( o->type ) {
    475451                        if ( ! type ) {
     
    494470
    495471                // there may be typedefs chained onto the type
    496                 if ( o->get_next() ) {
    497                         set_last( o->get_next()->clone() );
     472                if ( o->get_link() ) {
     473                        set_link( o->get_link()->clone() );
    498474                } // if
    499475        } // if
     
    718694        } // if
    719695        newnode->type->forall = maybeClone( type->forall );
    720         newnode->copyStorageClasses( this );
     696        newnode->storageClasses = storageClasses;
    721697        newnode->name = assign_strptr( newName );
    722698        return newnode;
     
    725701DeclarationNode *DeclarationNode::cloneBaseType( DeclarationNode *o ) {
    726702        if ( o ) {
    727                 o->copyStorageClasses( this );
     703                o->storageClasses.insert( o->storageClasses.end(), storageClasses.begin(), storageClasses.end() );
    728704                if ( type ) {
    729705                        TypeData *srcType = type;
     
    758734        DeclarationNode *newnode = new DeclarationNode;
    759735        newnode->type = maybeClone( type );
    760         newnode->copyStorageClasses( this );
     736        newnode->storageClasses = storageClasses;
    761737        newnode->name = assign_strptr( newName );
    762738        return newnode;
     
    765741DeclarationNode *DeclarationNode::cloneType( DeclarationNode *o ) {
    766742        if ( o ) {
    767                 o->copyStorageClasses( this );
     743                o->storageClasses.insert( o->storageClasses.end(), storageClasses.begin(), storageClasses.end() );
    768744                if ( type ) {
    769745                        TypeData *newType = type->clone();
     
    779755}
    780756
     757DeclarationNode *DeclarationNode::appendList( DeclarationNode *node ) {
     758        if ( node != 0 ) {
     759                set_link( node );
     760        } // if
     761        return this;
     762}
     763
    781764DeclarationNode *DeclarationNode::extractAggregate() const {
    782765        if ( type ) {
     
    793776void buildList( const DeclarationNode *firstNode, std::list< Declaration * > &outputList ) {
    794777        SemanticError errors;
    795         std::back_insert_iterator< std::list< Declaration * > > out( outputList );
     778        std::back_insert_iterator< std::list< Declaration *> > out( outputList );
    796779        const DeclarationNode *cur = firstNode;
    797780        while ( cur ) {
     
    811794                        errors.append( e );
    812795                } // try
    813                 cur = dynamic_cast< DeclarationNode * >( cur->get_next() );
     796                cur = dynamic_cast<DeclarationNode *>( cur->get_link() );
    814797        } // while
    815798        if ( ! errors.isEmpty() ) {
     
    818801}
    819802
    820 void buildList( const DeclarationNode *firstNode, std::list< DeclarationWithType * > &outputList ) {
     803void buildList( const DeclarationNode *firstNode, std::list< DeclarationWithType *> &outputList ) {
    821804        SemanticError errors;
    822         std::back_insert_iterator< std::list< DeclarationWithType * > > out( outputList );
     805        std::back_insert_iterator< std::list< DeclarationWithType *> > out( outputList );
    823806        const DeclarationNode *cur = firstNode;
    824807        while ( cur ) {
     
    834817                        Declaration *decl = cur->build();
    835818                        if ( decl ) {
    836                                 if ( DeclarationWithType *dwt = dynamic_cast< DeclarationWithType * >( decl ) ) {
     819                                if ( DeclarationWithType *dwt = dynamic_cast< DeclarationWithType *>( decl ) ) {
    837820                                        *out++ = dwt;
    838                                 } else if ( StructDecl *agg = dynamic_cast< StructDecl * >( decl ) ) {
     821                                } else if ( StructDecl *agg = dynamic_cast< StructDecl *>( decl ) ) {
    839822                                        StructInstType *inst = new StructInstType( Type::Qualifiers(), agg->get_name() );
    840823                                        *out++ = new ObjectDecl( "", DeclarationNode::NoStorageClass, linkage, 0, inst, 0 );
    841824                                        delete agg;
    842                                 } else if ( UnionDecl *agg = dynamic_cast< UnionDecl * >( decl ) ) {
     825                                } else if ( UnionDecl *agg = dynamic_cast< UnionDecl *>( decl ) ) {
    843826                                        UnionInstType *inst = new UnionInstType( Type::Qualifiers(), agg->get_name() );
    844827                                        *out++ = new ObjectDecl( "", DeclarationNode::NoStorageClass, linkage, 0, inst, 0 );
     
    848831                        errors.append( e );
    849832                } // try
    850                 cur = dynamic_cast< DeclarationNode * >( cur->get_next() );
     833                cur = dynamic_cast< DeclarationNode *>( cur->get_link() );
    851834        } // while
    852835        if ( ! errors.isEmpty() ) {
     
    855838}
    856839
    857 void buildTypeList( const DeclarationNode *firstNode, std::list< Type * > &outputList ) {
     840void buildTypeList( const DeclarationNode *firstNode, std::list< Type *> &outputList ) {
    858841        SemanticError errors;
    859         std::back_insert_iterator< std::list< Type * > > out( outputList );
     842        std::back_insert_iterator< std::list< Type *> > out( outputList );
    860843        const DeclarationNode *cur = firstNode;
    861844        while ( cur ) {
     
    865848                        errors.append( e );
    866849                } // try
    867                 cur = dynamic_cast< DeclarationNode * >( cur->get_next() );
     850                cur = dynamic_cast< DeclarationNode *>( cur->get_link() );
    868851        } // while
    869852        if ( ! errors.isEmpty() ) {
     
    873856
    874857Declaration *DeclarationNode::build() const {
    875         if( !error.empty() ) throw SemanticError( error, this );
    876858        if ( type ) {
    877                 return type->buildDecl( name, storageClass, maybeBuild< Expression >( bitfieldWidth ), isInline, isNoreturn, linkage, maybeBuild< Initializer >(initializer) )->set_extension( extension );
    878         } // if
    879         if ( ! isInline && ! isNoreturn ) {
    880                 return (new ObjectDecl( name, storageClass, linkage, maybeBuild< Expression >( bitfieldWidth ), 0, maybeBuild< Initializer >( initializer ) ))->set_extension( extension );
     859                return type->buildDecl( name, buildStorageClass(), maybeBuild< Expression >( bitfieldWidth ), buildFuncSpecifier( Inline ), buildFuncSpecifier( Noreturn ), linkage, maybeBuild< Initializer >(initializer) )->set_extension( extension );
     860        } // if
     861        if ( ! buildFuncSpecifier( Inline ) && ! buildFuncSpecifier( Noreturn ) ) {
     862                return (new ObjectDecl( name, buildStorageClass(), linkage, maybeBuild< Expression >( bitfieldWidth ), 0, maybeBuild< Initializer >( initializer ) ))->set_extension( extension );
    881863        } // if
    882864        throw SemanticError( "invalid function specifier in declaration of ", this );
     
    917899}
    918900
    919 // DeclarationNode::StorageClass DeclarationNode::buildStorageClass() const {
    920 //      DeclarationNode::StorageClass ret = DeclarationNode::NoStorageClass;
    921 //      for ( std::list< DeclarationNode::StorageClass >::const_iterator i = storageClasses.begin(); i != storageClasses.end(); ++i ) {
    922 //        if ( *i == DeclarationNode::Inline || *i == DeclarationNode::Noreturn ) continue; // ignore function specifiers
    923 //        if ( ret != DeclarationNode::NoStorageClass ) {       // already have a valid storage class ?
    924 //                      throw SemanticError( "invalid combination of storage classes in declaration of ", this );
    925 //              } // if
    926 //              ret = *i;
    927 //      } // for
    928 //      return ret;
    929 // }
    930 
    931 // bool DeclarationNode::buildFuncSpecifier( DeclarationNode::StorageClass key ) const {
    932 //      std::list< DeclarationNode::StorageClass >::const_iterator first = std::find( storageClasses.begin(), storageClasses.end(), key );
    933 //   if ( first == storageClasses.end() ) return false; // not found
    934 //      first = std::find( ++first, storageClasses.end(), key ); // found
    935 //   if ( first == storageClasses.end() ) return true;          // not found again
    936 //      throw SemanticError( "duplicate function specifier in declaration of ", this );
    937 // }
     901DeclarationNode::StorageClass DeclarationNode::buildStorageClass() const {
     902        DeclarationNode::StorageClass ret = DeclarationNode::NoStorageClass;
     903        for ( std::list< DeclarationNode::StorageClass >::const_iterator i = storageClasses.begin(); i != storageClasses.end(); ++i ) {
     904          if ( *i == DeclarationNode::Inline || *i == DeclarationNode::Noreturn ) continue; // ignore function specifiers
     905          if ( ret != DeclarationNode::NoStorageClass ) {       // already have a valid storage class ?
     906                        throw SemanticError( "invalid combination of storage classes in declaration of ", this );
     907                } // if
     908                ret = *i;
     909        } // for
     910        return ret;
     911}
     912
     913bool DeclarationNode::buildFuncSpecifier( DeclarationNode::StorageClass key ) const {
     914        std::list< DeclarationNode::StorageClass >::const_iterator first = std::find( storageClasses.begin(), storageClasses.end(), key );
     915  if ( first == storageClasses.end() ) return false;    // not found
     916        first = std::find( ++first, storageClasses.end(), key ); // found
     917  if ( first == storageClasses.end() ) return true;             // not found again
     918        throw SemanticError( "duplicate function specifier in declaration of ", this );
     919}
    938920
    939921// Local Variables: //
Note: See TracChangeset for help on using the changeset viewer.