Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Parser/DeclarationNode.cc

    rd1625f8 r413ad05  
    1010// Created On       : Sat May 16 12:34:05 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Tue Aug  9 08:39:20 2016
    13 // Update Count     : 169
     12// Last Modified On : Sun Aug 28 22:12:44 2016
     13// Update Count     : 278
    1414//
    1515
     
    2525#include "SynTree/Expression.h"
    2626
    27 #include "Parser.h"
    2827#include "TypedefTable.h"
    2928extern TypedefTable typedefTable;
     
    4241UniqueName DeclarationNode::anonymous( "__anonymous" );
    4342
    44 extern LinkageSpec::Type linkage;                                               // defined in parser.yy
     43extern LinkageSpec::Spec linkage;                                               // defined in parser.yy
    4544
    4645DeclarationNode *DeclarationNode::clone() const {
     
    4847        newnode->type = maybeClone( type );
    4948        newnode->name = name;
    50         newnode->storageClasses = storageClasses;
    51 //PAB   newnode->bitfieldWidth = maybeClone( bitfieldWidth );
    52         newnode->bitfieldWidth = bitfieldWidth;
     49        newnode->storageClass = storageClass;
     50        newnode->isInline = isInline;
     51        newnode->isNoreturn = isNoreturn;
     52        newnode->bitfieldWidth = maybeClone( bitfieldWidth );
    5353        newnode->hasEllipsis = hasEllipsis;
    54         newnode->initializer = initializer;
    55         newnode->next = maybeClone( next );
     54        newnode->initializer = maybeClone( initializer );
     55        newnode->set_next( maybeClone( get_next() ) );
    5656        newnode->linkage = linkage;
    5757        return newnode;
    5858} // DeclarationNode::clone
    5959
    60 DeclarationNode::DeclarationNode() : type( 0 ), bitfieldWidth( 0 ), initializer( 0 ), hasEllipsis( false ), linkage( ::linkage ) {
     60DeclarationNode::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() {
    6171}
    6272
     
    8393        } // if
    8494
    85         printEnums( storageClasses.begin(), storageClasses.end(), DeclarationNode::storageName, os );
     95        if ( storageClass != NoStorageClass ) os << DeclarationNode::storageName[storageClass] << ' ';
     96        if ( isInline ) os << DeclarationNode::storageName[Inline] << ' ';
     97        if ( isNoreturn ) os << DeclarationNode::storageName[Noreturn] << ' ';
    8698        if ( type ) {
    8799                type->print( os, indent );
     
    135147} // DeclarationNode::newFunction
    136148
    137 DeclarationNode *DeclarationNode::newQualifier( Qualifier q ) {
     149DeclarationNode * DeclarationNode::newQualifier( Qualifier q ) {
    138150        DeclarationNode *newnode = new DeclarationNode;
    139151        newnode->type = new TypeData();
    140         newnode->type->qualifiers.push_back( q );
     152        newnode->type->qualifiers[ q ] = 1;
    141153        return newnode;
    142154} // DeclarationNode::newQualifier
    143155
    144 DeclarationNode *DeclarationNode::newStorageClass( DeclarationNode::StorageClass sc ) {
    145         DeclarationNode *newnode = new DeclarationNode;
    146         newnode->storageClasses.push_back( sc );
     156DeclarationNode * DeclarationNode::newForall( DeclarationNode *forall ) {
     157        DeclarationNode *newnode = new DeclarationNode;
     158        newnode->type = new TypeData( TypeData::Unknown );
     159        newnode->type->forall = forall;
     160        return newnode;
     161} // DeclarationNode::newForall
     162
     163DeclarationNode * DeclarationNode::newStorageClass( DeclarationNode::StorageClass sc ) {
     164        DeclarationNode *newnode = new DeclarationNode;
     165        //switch (sc) {
     166        //      case Inline: newnode->isInline = true; break;
     167        //      case Noreturn: newnode->isNoreturn = true; break;
     168        //      default: newnode->storageClass = sc; break;
     169        //}
     170        newnode->storageClass = sc;
    147171        return newnode;
    148172} // DeclarationNode::newStorageClass
    149173
    150 DeclarationNode *DeclarationNode::newBasicType( BasicType bt ) {
     174DeclarationNode * DeclarationNode::newBasicType( BasicType bt ) {
    151175        DeclarationNode *newnode = new DeclarationNode;
    152176        newnode->type = new TypeData( TypeData::Basic );
     
    155179} // DeclarationNode::newBasicType
    156180
    157 DeclarationNode *DeclarationNode::newBuiltinType( BuiltinType bt ) {
     181DeclarationNode * DeclarationNode::newModifier( Modifier mod ) {
     182        DeclarationNode *newnode = new DeclarationNode;
     183        newnode->type = new TypeData( TypeData::Basic );
     184        newnode->type->basic->modifiers.push_back( mod );
     185        return newnode;
     186} // DeclarationNode::newModifier
     187
     188DeclarationNode * DeclarationNode::newBuiltinType( BuiltinType bt ) {
    158189        DeclarationNode *newnode = new DeclarationNode;
    159190        newnode->type = new TypeData( TypeData::Builtin );
     
    162193} // DeclarationNode::newBuiltinType
    163194
    164 DeclarationNode *DeclarationNode::newModifier( Modifier mod ) {
    165         DeclarationNode *newnode = new DeclarationNode;
    166         newnode->type = new TypeData( TypeData::Basic );
    167         newnode->type->basic->modifiers.push_back( mod );
    168         return newnode;
    169 } // DeclarationNode::newModifier
    170 
    171 DeclarationNode *DeclarationNode::newForall( DeclarationNode *forall ) {
    172         DeclarationNode *newnode = new DeclarationNode;
    173         newnode->type = new TypeData( TypeData::Unknown );
    174         newnode->type->forall = forall;
    175         return newnode;
    176 } // DeclarationNode::newForall
    177 
    178 DeclarationNode *DeclarationNode::newFromTypedef( std::string *name ) {
     195DeclarationNode * DeclarationNode::newFromTypedef( std::string *name ) {
    179196        DeclarationNode *newnode = new DeclarationNode;
    180197        newnode->type = new TypeData( TypeData::SymbolicInst );
     
    185202} // DeclarationNode::newFromTypedef
    186203
    187 DeclarationNode *DeclarationNode::newAggregate( Aggregate kind, const std::string *name, ExpressionNode *actuals, DeclarationNode *fields, bool body ) {
     204DeclarationNode * DeclarationNode::newAggregate( Aggregate kind, const std::string *name, ExpressionNode *actuals, DeclarationNode *fields, bool body ) {
    188205        DeclarationNode *newnode = new DeclarationNode;
    189206        newnode->type = new TypeData( TypeData::Aggregate );
     
    214231        DeclarationNode *newnode = new DeclarationNode;
    215232        newnode->name = assign_strptr( name );
    216         newnode->enumeratorValue = constant;
     233        newnode->enumeratorValue.reset( constant );
    217234        typedefTable.addToEnclosingScope( newnode->name, TypedefTable::ID );
    218235        return newnode;
     
    284301        newnode->type->array->dimension = size;
    285302        newnode->type->array->isStatic = isStatic;
    286         if ( newnode->type->array->dimension == 0 || dynamic_cast<ConstantExpr *>( newnode->type->array->dimension->build() ) ) {
     303        if ( newnode->type->array->dimension == 0 || newnode->type->array->dimension->isExpressionType<ConstantExpr *>() ) {
    287304                newnode->type->array->isVarLen = false;
    288305        } else {
     
    353370                        src = 0;
    354371                } else {
    355                         dst->qualifiers.splice( dst->qualifiers.end(), src->qualifiers );
    356                 } // if
    357         } // if
    358 }
     372                        dst->qualifiers |= src->qualifiers;
     373                } // if
     374        } // if
     375}
     376
     377void DeclarationNode::checkQualifiers( const TypeData *src, const TypeData *dst ) {
     378        TypeData::Qualifiers qsrc = src->qualifiers, qdst = dst->qualifiers;
     379
     380        if ( (qsrc & qdst).any() ) {                                            // common bits between qualifier masks ?
     381                error = "duplicate qualifier ";
     382                int j = 0;                                                                              // separator detector
     383                for ( int i = 0; i < DeclarationNode::NoOfQualifier; i += 1 ) {
     384                        if ( qsrc[i] & qdst[i] ) {                                      // find specific qualifiers in common
     385                                if ( j > 0 ) error += ", ";
     386                                error += DeclarationNode::qualifierName[i];
     387                                j += 1;
     388                        } // if
     389                } // for
     390                error += " in declaration of ";
     391        } // if
     392} // DeclarationNode::checkQualifiers
    359393
    360394DeclarationNode *DeclarationNode::addQualifiers( DeclarationNode *q ) {
    361395        if ( q ) {
    362                 storageClasses.splice( storageClasses.end(), q->storageClasses );
     396                copyStorageClasses(q);
    363397                if ( q->type ) {
    364398                        if ( ! type ) {
    365399                                type = new TypeData;
     400                        } else {
     401                                checkQualifiers( q->type, type );
    366402                        } // if
    367403                        addQualifiersToType( q->type, type );
     
    387423
    388424DeclarationNode *DeclarationNode::copyStorageClasses( DeclarationNode *q ) {
    389         storageClasses = q->storageClasses;
     425        isInline = isInline || q->isInline;
     426        isNoreturn = isNoreturn || q->isNoreturn;
     427        if ( storageClass == NoStorageClass ) {
     428                storageClass = q->storageClass;
     429        } else if ( q->storageClass != NoStorageClass ) {
     430                q->error = "invalid combination of storage classes in declaration of ";
     431        } // if
     432        if ( error.empty() ) error = q->error;
    390433        return this;
    391434}
     
    406449                        switch ( dst->kind ) {
    407450                          case TypeData::Unknown:
    408                                 src->qualifiers.splice( src->qualifiers.end(), dst->qualifiers );
     451                                src->qualifiers |= dst->qualifiers;
    409452                                dst = src;
    410453                                src = 0;
    411454                                break;
    412455                          case TypeData::Basic:
    413                                 dst->qualifiers.splice( dst->qualifiers.end(), src->qualifiers );
     456                                dst->qualifiers |= src->qualifiers;
    414457                                if ( src->kind != TypeData::Unknown ) {
    415458                                        assert( src->kind == TypeData::Basic );
     
    427470                                                dst->base->aggInst->params = maybeClone( src->aggregate->actuals );
    428471                                        } // if
    429                                         dst->base->qualifiers.splice( dst->base->qualifiers.end(), src->qualifiers );
     472                                        dst->base->qualifiers |= src->qualifiers;
    430473                                        src = 0;
    431474                                        break;
     
    447490DeclarationNode *DeclarationNode::addType( DeclarationNode *o ) {
    448491        if ( o ) {
    449                 storageClasses.splice( storageClasses.end(), o->storageClasses );
     492                copyStorageClasses( o );
    450493                if ( o->type ) {
    451494                        if ( ! type ) {
     
    456499                                                type->aggInst->params = maybeClone( o->type->aggregate->actuals );
    457500                                        } // if
    458                                         type->qualifiers.splice( type->qualifiers.end(), o->type->qualifiers );
     501                                        type->qualifiers |= o->type->qualifiers;
    459502                                } else {
    460503                                        type = o->type;
     
    470513
    471514                // there may be typedefs chained onto the type
    472                 if ( o->get_link() ) {
    473                         set_link( o->get_link()->clone() );
     515                if ( o->get_next() ) {
     516                        set_last( o->get_next()->clone() );
    474517                } // if
    475518        } // if
     
    591634                                        p->type->base->aggInst->params = maybeClone( type->aggregate->actuals );
    592635                                } // if
    593                                 p->type->base->qualifiers.splice( p->type->base->qualifiers.end(), type->qualifiers );
     636                                p->type->base->qualifiers |= type->qualifiers;
    594637                                break;
    595638
     
    628671                                        lastArray->base->aggInst->params = maybeClone( type->aggregate->actuals );
    629672                                } // if
    630                                 lastArray->base->qualifiers.splice( lastArray->base->qualifiers.end(), type->qualifiers );
     673                                lastArray->base->qualifiers |= type->qualifiers;
    631674                                break;
    632675                          default:
     
    694737        } // if
    695738        newnode->type->forall = maybeClone( type->forall );
    696         newnode->storageClasses = storageClasses;
     739        newnode->copyStorageClasses( this );
    697740        newnode->name = assign_strptr( newName );
    698741        return newnode;
     
    701744DeclarationNode *DeclarationNode::cloneBaseType( DeclarationNode *o ) {
    702745        if ( o ) {
    703                 o->storageClasses.insert( o->storageClasses.end(), storageClasses.begin(), storageClasses.end() );
     746                o->copyStorageClasses( this );
    704747                if ( type ) {
    705748                        TypeData *srcType = type;
     
    734777        DeclarationNode *newnode = new DeclarationNode;
    735778        newnode->type = maybeClone( type );
    736         newnode->storageClasses = storageClasses;
     779        newnode->copyStorageClasses( this );
    737780        newnode->name = assign_strptr( newName );
    738781        return newnode;
     
    741784DeclarationNode *DeclarationNode::cloneType( DeclarationNode *o ) {
    742785        if ( o ) {
    743                 o->storageClasses.insert( o->storageClasses.end(), storageClasses.begin(), storageClasses.end() );
     786                o->copyStorageClasses( this );
    744787                if ( type ) {
    745788                        TypeData *newType = type->clone();
     
    752795                } // if
    753796        } // if
     797        delete o;
    754798        return o;
    755 }
    756 
    757 DeclarationNode *DeclarationNode::appendList( DeclarationNode *node ) {
    758         if ( node != 0 ) {
    759                 set_link( node );
    760         } // if
    761         return this;
    762799}
    763800
    764801DeclarationNode *DeclarationNode::extractAggregate() const {
    765802        if ( type ) {
    766                 TypeData *ret = type->extractAggregate();
     803                TypeData *ret = typeextractAggregate( type );
    767804                if ( ret ) {
    768805                        DeclarationNode *newnode = new DeclarationNode;
     
    776813void buildList( const DeclarationNode *firstNode, std::list< Declaration * > &outputList ) {
    777814        SemanticError errors;
    778         std::back_insert_iterator< std::list< Declaration *> > out( outputList );
     815        std::back_insert_iterator< std::list< Declaration * > > out( outputList );
    779816        const DeclarationNode *cur = firstNode;
    780817        while ( cur ) {
     
    786823                                        *out++ = decl;
    787824                                } // if
     825                                delete extr;
    788826                        } // if
    789827                        Declaration *decl = cur->build();
     
    794832                        errors.append( e );
    795833                } // try
    796                 cur = dynamic_cast<DeclarationNode *>( cur->get_link() );
     834                cur = dynamic_cast< DeclarationNode * >( cur->get_next() );
    797835        } // while
    798836        if ( ! errors.isEmpty() ) {
     
    801839}
    802840
    803 void buildList( const DeclarationNode *firstNode, std::list< DeclarationWithType *> &outputList ) {
     841void buildList( const DeclarationNode *firstNode, std::list< DeclarationWithType * > &outputList ) {
    804842        SemanticError errors;
    805         std::back_insert_iterator< std::list< DeclarationWithType *> > out( outputList );
     843        std::back_insert_iterator< std::list< DeclarationWithType * > > out( outputList );
    806844        const DeclarationNode *cur = firstNode;
    807845        while ( cur ) {
     
    817855                        Declaration *decl = cur->build();
    818856                        if ( decl ) {
    819                                 if ( DeclarationWithType *dwt = dynamic_cast< DeclarationWithType *>( decl ) ) {
     857                                if ( DeclarationWithType *dwt = dynamic_cast< DeclarationWithType * >( decl ) ) {
    820858                                        *out++ = dwt;
    821                                 } else if ( StructDecl *agg = dynamic_cast< StructDecl *>( decl ) ) {
     859                                } else if ( StructDecl *agg = dynamic_cast< StructDecl * >( decl ) ) {
    822860                                        StructInstType *inst = new StructInstType( Type::Qualifiers(), agg->get_name() );
    823861                                        *out++ = new ObjectDecl( "", DeclarationNode::NoStorageClass, linkage, 0, inst, 0 );
    824862                                        delete agg;
    825                                 } else if ( UnionDecl *agg = dynamic_cast< UnionDecl *>( decl ) ) {
     863                                } else if ( UnionDecl *agg = dynamic_cast< UnionDecl * >( decl ) ) {
    826864                                        UnionInstType *inst = new UnionInstType( Type::Qualifiers(), agg->get_name() );
    827865                                        *out++ = new ObjectDecl( "", DeclarationNode::NoStorageClass, linkage, 0, inst, 0 );
     
    831869                        errors.append( e );
    832870                } // try
    833                 cur = dynamic_cast< DeclarationNode *>( cur->get_link() );
     871                cur = dynamic_cast< DeclarationNode * >( cur->get_next() );
    834872        } // while
    835873        if ( ! errors.isEmpty() ) {
     
    838876}
    839877
    840 void buildTypeList( const DeclarationNode *firstNode, std::list< Type *> &outputList ) {
     878void buildTypeList( const DeclarationNode *firstNode, std::list< Type * > &outputList ) {
    841879        SemanticError errors;
    842         std::back_insert_iterator< std::list< Type *> > out( outputList );
     880        std::back_insert_iterator< std::list< Type * > > out( outputList );
    843881        const DeclarationNode *cur = firstNode;
    844882        while ( cur ) {
     
    848886                        errors.append( e );
    849887                } // try
    850                 cur = dynamic_cast< DeclarationNode *>( cur->get_link() );
     888                cur = dynamic_cast< DeclarationNode * >( cur->get_next() );
    851889        } // while
    852890        if ( ! errors.isEmpty() ) {
     
    856894
    857895Declaration *DeclarationNode::build() const {
     896        if ( ! error.empty() ) throw SemanticError( error, this );
    858897        if ( type ) {
    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 );
     898                return buildDecl( type, name, storageClass, maybeBuild< Expression >( bitfieldWidth ), isInline, isNoreturn, linkage, maybeBuild< Initializer >(initializer) )->set_extension( extension );
     899        } // if
     900        if ( ! isInline && ! isNoreturn ) {
     901                return (new ObjectDecl( name, storageClass, linkage, maybeBuild< Expression >( bitfieldWidth ), 0, maybeBuild< Initializer >( initializer ) ))->set_extension( extension );
    863902        } // if
    864903        throw SemanticError( "invalid function specifier in declaration of ", this );
     
    870909        switch ( type->kind ) {
    871910          case TypeData::Enum:
    872                 return new EnumInstType( type->buildQualifiers(), type->enumeration->name );
     911                return new EnumInstType( buildQualifiers( type ), type->enumeration->name );
    873912          case TypeData::Aggregate: {
    874913                  ReferenceToType *ret;
    875914                  switch ( type->aggregate->kind ) {
    876915                        case DeclarationNode::Struct:
    877                           ret = new StructInstType( type->buildQualifiers(), type->aggregate->name );
     916                          ret = new StructInstType( buildQualifiers( type ), type->aggregate->name );
    878917                          break;
    879918                        case DeclarationNode::Union:
    880                           ret = new UnionInstType( type->buildQualifiers(), type->aggregate->name );
     919                          ret = new UnionInstType( buildQualifiers( type ), type->aggregate->name );
    881920                          break;
    882921                        case DeclarationNode::Trait:
    883                           ret = new TraitInstType( type->buildQualifiers(), type->aggregate->name );
     922                          ret = new TraitInstType( buildQualifiers( type ), type->aggregate->name );
    884923                          break;
    885924                        default:
     
    890929          }
    891930          case TypeData::Symbolic: {
    892                   TypeInstType *ret = new TypeInstType( type->buildQualifiers(), type->symbolic->name, false );
     931                  TypeInstType *ret = new TypeInstType( buildQualifiers( type ), type->symbolic->name, false );
    893932                  buildList( type->symbolic->actuals, ret->get_parameters() );
    894933                  return ret;
    895934          }
    896935          default:
    897                 return type->build();
     936                return typebuild( type );
    898937        } // switch
    899938}
    900939
    901 DeclarationNode::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 
    913 bool 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 }
     940// DeclarationNode::StorageClass DeclarationNode::buildStorageClass() const {
     941//      DeclarationNode::StorageClass ret = DeclarationNode::NoStorageClass;
     942//      for ( std::list< DeclarationNode::StorageClass >::const_iterator i = storageClasses.begin(); i != storageClasses.end(); ++i ) {
     943//        if ( *i == DeclarationNode::Inline || *i == DeclarationNode::Noreturn ) continue; // ignore function specifiers
     944//        if ( ret != DeclarationNode::NoStorageClass ) {       // already have a valid storage class ?
     945//                      throw SemanticError( "invalid combination of storage classes in declaration of ", this );
     946//              } // if
     947//              ret = *i;
     948//      } // for
     949//      return ret;
     950// }
     951
     952// bool DeclarationNode::buildFuncSpecifier( DeclarationNode::StorageClass key ) const {
     953//      std::list< DeclarationNode::StorageClass >::const_iterator first = std::find( storageClasses.begin(), storageClasses.end(), key );
     954//   if ( first == storageClasses.end() ) return false; // not found
     955//      first = std::find( ++first, storageClasses.end(), key ); // found
     956//   if ( first == storageClasses.end() ) return true;          // not found again
     957//      throw SemanticError( "duplicate function specifier in declaration of ", this );
     958// }
    920959
    921960// Local Variables: //
Note: See TracChangeset for help on using the changeset viewer.