Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Parser/DeclarationNode.cc

    r2037f82 re82aa9df  
    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 : Mon Aug 15 14:30:25 2016
     13// Update Count     : 172
    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<ConstantExpr *>( newnode->type->array->dimension->build() ) ) {
    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 ) {
     
    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();
     
    776752                } // if
    777753        } // if
    778         delete o;
    779754        return o;
     755}
     756
     757DeclarationNode *DeclarationNode::appendList( DeclarationNode *node ) {
     758        if ( node != 0 ) {
     759                set_last( node );
     760        } // if
     761        return this;
    780762}
    781763
     
    794776void buildList( const DeclarationNode *firstNode, std::list< Declaration * > &outputList ) {
    795777        SemanticError errors;
    796         std::back_insert_iterator< std::list< Declaration * > > out( outputList );
     778        std::back_insert_iterator< std::list< Declaration *> > out( outputList );
    797779        const DeclarationNode *cur = firstNode;
    798780        while ( cur ) {
     
    812794                        errors.append( e );
    813795                } // try
    814                 cur = dynamic_cast< DeclarationNode * >( cur->get_next() );
     796                cur = dynamic_cast<DeclarationNode *>( cur->get_next() );
    815797        } // while
    816798        if ( ! errors.isEmpty() ) {
     
    819801}
    820802
    821 void buildList( const DeclarationNode *firstNode, std::list< DeclarationWithType * > &outputList ) {
     803void buildList( const DeclarationNode *firstNode, std::list< DeclarationWithType *> &outputList ) {
    822804        SemanticError errors;
    823         std::back_insert_iterator< std::list< DeclarationWithType * > > out( outputList );
     805        std::back_insert_iterator< std::list< DeclarationWithType *> > out( outputList );
    824806        const DeclarationNode *cur = firstNode;
    825807        while ( cur ) {
     
    835817                        Declaration *decl = cur->build();
    836818                        if ( decl ) {
    837                                 if ( DeclarationWithType *dwt = dynamic_cast< DeclarationWithType * >( decl ) ) {
     819                                if ( DeclarationWithType *dwt = dynamic_cast< DeclarationWithType *>( decl ) ) {
    838820                                        *out++ = dwt;
    839                                 } else if ( StructDecl *agg = dynamic_cast< StructDecl * >( decl ) ) {
     821                                } else if ( StructDecl *agg = dynamic_cast< StructDecl *>( decl ) ) {
    840822                                        StructInstType *inst = new StructInstType( Type::Qualifiers(), agg->get_name() );
    841823                                        *out++ = new ObjectDecl( "", DeclarationNode::NoStorageClass, linkage, 0, inst, 0 );
    842824                                        delete agg;
    843                                 } else if ( UnionDecl *agg = dynamic_cast< UnionDecl * >( decl ) ) {
     825                                } else if ( UnionDecl *agg = dynamic_cast< UnionDecl *>( decl ) ) {
    844826                                        UnionInstType *inst = new UnionInstType( Type::Qualifiers(), agg->get_name() );
    845827                                        *out++ = new ObjectDecl( "", DeclarationNode::NoStorageClass, linkage, 0, inst, 0 );
     
    849831                        errors.append( e );
    850832                } // try
    851                 cur = dynamic_cast< DeclarationNode * >( cur->get_next() );
     833                cur = dynamic_cast< DeclarationNode *>( cur->get_next() );
    852834        } // while
    853835        if ( ! errors.isEmpty() ) {
     
    856838}
    857839
    858 void buildTypeList( const DeclarationNode *firstNode, std::list< Type * > &outputList ) {
     840void buildTypeList( const DeclarationNode *firstNode, std::list< Type *> &outputList ) {
    859841        SemanticError errors;
    860         std::back_insert_iterator< std::list< Type * > > out( outputList );
     842        std::back_insert_iterator< std::list< Type *> > out( outputList );
    861843        const DeclarationNode *cur = firstNode;
    862844        while ( cur ) {
     
    866848                        errors.append( e );
    867849                } // try
    868                 cur = dynamic_cast< DeclarationNode * >( cur->get_next() );
     850                cur = dynamic_cast< DeclarationNode *>( cur->get_next() );
    869851        } // while
    870852        if ( ! errors.isEmpty() ) {
     
    874856
    875857Declaration *DeclarationNode::build() const {
    876         if( !error.empty() ) throw SemanticError( error, this );
    877858        if ( type ) {
    878                 return type->buildDecl( name, storageClass, maybeBuild< Expression >( bitfieldWidth ), isInline, isNoreturn, linkage, maybeBuild< Initializer >(initializer) )->set_extension( extension );
    879         } // if
    880         if ( ! isInline && ! isNoreturn ) {
    881                 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 );
    882863        } // if
    883864        throw SemanticError( "invalid function specifier in declaration of ", this );
     
    918899}
    919900
    920 // DeclarationNode::StorageClass DeclarationNode::buildStorageClass() const {
    921 //      DeclarationNode::StorageClass ret = DeclarationNode::NoStorageClass;
    922 //      for ( std::list< DeclarationNode::StorageClass >::const_iterator i = storageClasses.begin(); i != storageClasses.end(); ++i ) {
    923 //        if ( *i == DeclarationNode::Inline || *i == DeclarationNode::Noreturn ) continue; // ignore function specifiers
    924 //        if ( ret != DeclarationNode::NoStorageClass ) {       // already have a valid storage class ?
    925 //                      throw SemanticError( "invalid combination of storage classes in declaration of ", this );
    926 //              } // if
    927 //              ret = *i;
    928 //      } // for
    929 //      return ret;
    930 // }
    931 
    932 // bool DeclarationNode::buildFuncSpecifier( DeclarationNode::StorageClass key ) const {
    933 //      std::list< DeclarationNode::StorageClass >::const_iterator first = std::find( storageClasses.begin(), storageClasses.end(), key );
    934 //   if ( first == storageClasses.end() ) return false; // not found
    935 //      first = std::find( ++first, storageClasses.end(), key ); // found
    936 //   if ( first == storageClasses.end() ) return true;          // not found again
    937 //      throw SemanticError( "duplicate function specifier in declaration of ", this );
    938 // }
     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}
    939920
    940921// Local Variables: //
Note: See TracChangeset for help on using the changeset viewer.